Codeforces--598A--Tricky Sum(数学)
2016-03-12 13:10
281 查看
Tricky Sum
Tricky SumCrawling in process...
Crawling failed
Time Limit:1000MS
Memory Limit:262144KB
64bit IO Format:%I64d & %I64u
Submit
Status
Practice
CodeForces 598A
Description
In this problem you are to calculate the sum of all integers from
1 to n, but you should take all powers of two with minus in the sum.
For example, for n = 4 the sum is equal to
- 1 - 2 + 3 - 4 = - 4, because
1, 2 and 4 are
20,
21 and
22 respectively.
Calculate the answer for t values of
n.
Input
The first line of the input contains a single integer t (1 ≤ t ≤ 100) — the number of values of
n to be processed.
Each of next t lines contains a single integer
n (1 ≤ n ≤ 109).
Output
Print the requested sum for each of t integers
n given in the input.
Sample Input
Input
Output
Sample Output
Hint
The answer for the first sample is explained in the statement.
1--n全部加起来,如果是2的次幂 数字就为负的,求最后的和,先利用等差序列公式(n+1)*n/2,然后扣除2的次幂
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
__int64 n;
cin>>n;
__int64 ans=(n+1)*n/2-2;
for(int i=1;pow(2,i)<=n;i++)
ans-=pow(2,i+1);
cout<<ans<<endl;
}
return 0;
}
Tricky Sum
Tricky SumCrawling in process...
Crawling failed
Time Limit:1000MS
Memory Limit:262144KB
64bit IO Format:%I64d & %I64u
Submit
Status
Practice
CodeForces 598A
Description
In this problem you are to calculate the sum of all integers from
1 to n, but you should take all powers of two with minus in the sum.
For example, for n = 4 the sum is equal to
- 1 - 2 + 3 - 4 = - 4, because
1, 2 and 4 are
20,
21 and
22 respectively.
Calculate the answer for t values of
n.
Input
The first line of the input contains a single integer t (1 ≤ t ≤ 100) — the number of values of
n to be processed.
Each of next t lines contains a single integer
n (1 ≤ n ≤ 109).
Output
Print the requested sum for each of t integers
n given in the input.
Sample Input
Input
2 4 1000000000
Output
-4 499999998352516354
Sample Output
Hint
The answer for the first sample is explained in the statement.
1--n全部加起来,如果是2的次幂 数字就为负的,求最后的和,先利用等差序列公式(n+1)*n/2,然后扣除2的次幂
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
__int64 n;
cin>>n;
__int64 ans=(n+1)*n/2-2;
for(int i=1;pow(2,i)<=n;i++)
ans-=pow(2,i+1);
cout<<ans<<endl;
}
return 0;
}
相关文章推荐
- 构建之法阅读笔记02
- 1014-32-首页13-cell的结构分析---导航栏中间title位置的按钮的尺寸设置---setFrame----
- Codeforces--598A--Tricky Sum(数学)
- 如何让域中的计算机开机自动映射网络上的共享文件夹
- kidd风的IOS日志之Source Tree的使用教程
- 2013551813 张燕燕 第二周作业
- iOS之Runtime
- HighCharts 详细使用及API文档说明
- 为AppDelegate分层的面向服务架构的 SOAComponentAppDelegate
- “1010”问卷调查分析结果及心得体会
- CODE[VS] 1079 回家
- 送给前线码农的话 - 大牛们的经典语录
- 剑指offer-连续子数组的最大和----编程之美-连续子数组的最大和
- CentOs6.5中安装和配置vsftp简明教程
- xUtils更新到3.0后的基本使用规则
- 全虚拟化和半虚拟化的区别 cpu的ring0 ring1又是什么概念?
- JVM梳理
- 从开发者角度解析 Android N 新特性!
- 从开发者角度解析 Android N 新特性!
- shell简介