codeforces 598A Tricky Sum

题目链接http://codeforces.com/contest/598/problem/A

题目分类:大数

题意:1到n 如果是2的次方则减去这个数,否则就加上这个数,求最后的结果是多少

题目分析:数很大,关键是精度问题,刚开始用__int64和double发现都是不对的,后来发现用long long 可以过

代码

#include<bits/stdc++.h>

using namespace std;

#define LL long long

LL a[40]={0,1,2,4,8,16,32,64,128,
256,512,1024,2048,4096,8192,
16384,32768,65536,131072,262144,524288,
1048576,2097152,4194304,8388608,16777216,
33554432,67108864,134217728,268435456,536870912};

int main()
{
    int t;
    LL n;
    LL ans;
    cin>>t;
    while(t--)
    {
        cin>>n;
        ans=n*(1+n)/2;
        //cout<<ans<<endl;
        for(int i=1;i<=40;i++)
        {
            if(n>=a[i])
            {
                ans-=2*a[i];
            }
            else
            {
                break;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}
anytime you feel the pain.hey,refrain.don't carry the world upon your shoulders
原文地址:https://www.cnblogs.com/gaoss/p/4970432.html