当前位置:网站首页>Summation of basic exercise sequence of test questions
Summation of basic exercise sequence of test questions
2022-06-30 05:54:00 【_ Milestone】
Resource constraints
The time limit :1.0s Memory limit :256.0MB
Problem description
seek 1+2+3+...+n Value .
Input format
Input includes an integer n.
Output format
Output one line , Include an integer , Express 1+2+3+...+n Value .
The sample input
4
Sample output
10
The sample input
100
Sample output
5050
Data scale and agreement
1 <= n <= 1,000,000,000.
explain : Notice the size of the data here .
It's easy to get wrong : The direct idea of this problem is to directly use a cycle to accumulate , However , When the data scale is large , such “ violence ” This method often results in timeouts .
Another point worth noting is that the size of the answer is not in the default integer of your language (int) Within the scope of , If you use integers to save results , It can lead to wrong results . Need to use long long Type integer .
The core idea :
To use the summation formula of an arithmetic sequence :

Code :
#include<iostream>
using namespace std;
int main()
{
long long int num;
long long int sum=0;
cin>>num;
sum = (1+num)*num/2;
cout<<sum;
return 0;
}边栏推荐
- Database SQL language 06 single line function
- D. Big Brush
- How to automatically renew a token after it expires?
- Database SQL language 04 subquery and grouping function
- Solidy - fallback function - 2 trigger execution modes
- Sword finger offer 18 Delete the node of the linked list
- Codeforces B. MEX and Array
- El table lazy load refresh
- Database SQL language 05 SQL exercise
- 【LeetCode】236. Nearest common ancestor of binary tree
猜你喜欢

At the beginning of 2022, people who are ready to change jobs should pay attention to

PC viewing WiFi password
![[GPU] basic operation of GPU (I)](/img/ce/0ca8c63525038fea64c40aabd17fc6.jpg)
[GPU] basic operation of GPU (I)

How does WPS cancel automatic numbering? Four options

MySQL高级SQL语句
![[OSPF] comparison between rip and OSPF](/img/72/00e3a05bc5de0e5a66b4675d030911.jpg)
[OSPF] comparison between rip and OSPF

Configure the user to log in to the device through telnet -- AAA local authentication

Use of OpenCL thread algebra library viennacl

声网,站在物联网的“土壤”里

1380. lucky numbers in matrices
随机推荐
8 ways to earn passive income
ECS deployment web project
luoguP2756 飞行员配对方案问题(最大流)
Related applications of priority queue
El table lazy load refresh
Xiaosha's lunch
D. Big Brush
剑指 Offer 22. 链表中倒数第k个节点
Implementation of property management system with ssm+ wechat applet
Finally someone can make the server so straightforward
AI大模型落地大考,浪潮交出了怎样的答卷?
Database SQL language 05 SQL exercise
09- [istio] istio service entry
MySQL日志管理、数据备份、恢复
What are membrane stress and membrane strain
Ultra simple STM32 RTC alarm clock configuration
How to automatically renew a token after it expires?
C. Divan and bitwise operations
Do you know how to show the health code in only 2 steps
2022年,谁在推动音视频产业的新拐点?