A - A 使用long long
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
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.
Output
Print the requested sum for each of t integers n given in the input.
Hint
The answer for the first sample is explained in the statement.
#include<cstdio>
#include<cmath>
#include<algorithm>
#define LL long long
using namespace std;
LL a[50];
void init(){
for(int i=0;i<=32;i++){
a[i]=pow(2,i);
}
}
int main(){
LL n,t;
scanf("%lld",&t);
init();
while(t--){
LL sum,num=0;
scanf("%lld",&n);
sum=n*(n+1)/2; //等差数列
// printf("%lld--\n",sum);
for(int l=0;l<=32&&a[l]<=n;l++){
num=num+a[l];
}
printf("%lld\n",sum-num*2);
}
return 0;
}