Tricky sum
来源:互联网 发布:数据分析员工作总结 编辑:程序博客网 时间:2024/05/21 15:05
Tricky sum
Crawling in process...Crawling failedTime Limit:100MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
In this problem you are to calculate the sum of all integers from1 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 are20, 21 and 22 respectively.
Calculate the answer for t values ofn.
Input
The first line of the input contains a single integert (1 ≤ t ≤ 100) — the number of values ofn to be processed.
Each of next t lines contains a single integern (1 ≤ n ≤ 109).
Output
Print the requested sum for each of t integers n given in the input.
Sample Input
Input
241000000000
Output
-4499999998352516354
解释:例如:n=4时,1+2+3+4—(2^0+2^1+2^2)-(2^0+2^1+2^2)=-4;
既是(1+2+3+……+n)-2*(2^0+2^1+2^2+......) 所以公式中有个2;
代码:
#include<stdio.h> #include<math.h>int main(){int t;long long n;scanf("%d",&t);while(t--){scanf("%lld",&n);long long sum=n*(n+1)/2;for(int i=0;pow(2,i)<=n;i++){sum-=2*pow(2,i);}printf("%lld\n",sum);} return 0;}
0 0
- Tricky sum
- Tricky Sum
- Tricky Sum
- Tricky Sum
- Tricky Sum
- Tricky Sum
- Tricky Sum
- Tricky Sum
- Tricky Sum
- Tricky Sum
- CF--A. Tricky Sum
- A. Tricky Sum
- Tricky Sum(等比数列)
- codeforces_598A. Tricky Sum
- 【codeforces】Tricky Sum
- B - Tricky Sum
- 【codeforces】Tricky Sum
- codeforces - Tricky Sum(模拟)
- ulua学习笔记1
- ADB常用命令
- Q4:替换空格
- Android进阶(二十五)setTextColor()的参数设置方式
- Android圆形头像的绘制(二)之自定义视图
- Tricky sum
- linux vim程序编辑器-vim1
- 兼容各浏览器的Zero Clipboard js+swf实现的复制功能使用方法
- 运动检测ViBe算法python实现
- urlrewrite 中文乱码问题
- 消息推送
- dhtml技术(六)表单的验证 16.6.20(6.formsubimit)
- 总结_杭电1001
- 现代Web开发需要学习的15大技术