A. Tricky Sum
time limit per test
memory limit per test
input
output
1 to n, but you should take all powers of two with minus in the sum.
n = 4 the sum is equal to - 1 - 2 + 3 - 4 = - 4, because 1, 2 and 4 are 20, 21 and 22
t values of n.
Input
t (1 ≤ t ≤ 100) — the number of values of n
t lines contains a single integer n (1 ≤ n ≤ 109).
Output
t integers n
Sample test(s)
input
2 4 1000000000
output
-4 499999998352516354
Note
The answer for the first sample is explained in the statement.
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
__int64 n,sum,res=1,a=1;
scanf("%I64d",&n);
sum=(n*(n+1))/2;
for(__int64 i=1;i<35;i++)
{
a*=2;
if(a<=n)
res+=a;
else
break;
}
printf("%I64d\n",sum-2*res);
}
return 0;
}