coderforce 598A. Tricky Sum(math)

题意:计算1-n的和规则:如为2的次方,则sum-=i,否则为sum+=i;最后求sum;

解:可以把1-n的和全部求出来,再减去其中为2的次方数的2倍就是sum;

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define LL long long
int P(int k)
{
    int sum=1;
    for(int i=1;i<=k;i++)
    {
        sum*=2;
    }
    return sum;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        LL n;
        scanf("%lld",&n);
        LL sum=(n*n+n)/2;
        for(LL i=0;P(i)<=n;i++)
        {
            sum-=2*P(i);
        }
        printf("%lld\n",sum);
    }
    return 0;
}


你可能感兴趣的:(coderforce 598A. Tricky Sum(math))