Educational Codeforces Round 1 A. Tricky Sum(简单模拟求和)

来源:互联网 发布:淘宝外贸原单店铺推荐 编辑:程序博客网 时间:2024/06/06 10:53

题目链接
题意:求出1-n的和,但是要去掉是2的整次幂的数
解法:直接模拟即可

#include<bits/stdc++.h>using namespace std;#define LL long long#define pb push_back#define X first#define Y second#define cl(a,b) memset(a,b,sizeof(a))typedef pair<int,int> P;const int maxn=500005;const int inf=1<<27;#define mod 1000000007int main(){    int T;    scanf("%d",&T);    while(T--){        LL n;        scanf("%lld",&n);        LL sum=n*(n+1)/2;        LL t=0,x=1;        while(x<=n){            sum-=x*2;            t++;            x=1<<t;        }        printf("%lld\n",sum);    }    return 0;}
0 0
原创粉丝点击