Tricky Sum
来源:互联网 发布:网络语鸡肋是什么意思 编辑:程序博客网 时间:2024/05/28 16:06
Tricky Sum
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.
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).
Print the requested sum for each of t integers n given in the input.
Input
241000000000
Output
-4499999998352516354
The answer for the first sample is explained in the statement.
等差数列和等比数列的运用,容易想到等差,不容易想到等比
#include <iostream>#include <cstdio>int main(){ int t; scanf("%d",&t); while(t--){ long long int n,t; scanf("%lld",&n); long long int sum=0,cout=1,ans=0; sum=n*(n+1)/2; while(n) { n>>=1; ans++; } for(int i=0;i<ans;i++) { cout*=2; } cout=cout-1; sum=sum-2*cout; printf("%lld\n",sum); }}
阅读全文
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(模拟)
- Kylin+superset可视化
- source insight 4.0中宏定义做为函数返回值时,函数识别错误
- linux驱动由浅入深系列:camera驱动之一(背景及基础知识篇)
- fork/join模式中fork和invokeAll的区别
- 点九图使用
- Tricky Sum
- nyoj LYQの字符串(尺取法)
- 静态变量、全局变量和局部变量
- react native 编写Hello World的解析
- document.write和innerHTML的区别
- 1037. 在霍格沃茨找零钱
- Windows下TensorFlow环境搭建(无GPU)
- Python常用操作记录
- JMeter源码集成到Eclipse