Tricky sum

来源:互联网 发布:数据分析员工作总结 编辑:程序博客网 时间:2024/05/21 15:05

Tricky sum

Crawling in process...Crawling failedTime Limit:100MS    Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

SubmitStatus

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
原创粉丝点击