首页 > 学院 > 开发设计 > 正文

Code For 1

2019-11-08 01:14:00
字体:
来源:转载
供稿:网友
/*Divide by Zero 2017 and Codeforces Round #399Code For 1时间: 2017/02/21题意:将一个数2^50,分半划分成0,1后,问在l,r区间1的数量题解:发现1,0出现的位置是和n的二进制有关,具体:满足i%(2^xi)==0的最大xi,第i个位置的数就是二进制从后往前的第xi个二进制值。注意 位运算1<<n 在n较大时时间花费巨大*/#include<cstdio>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>#include<queue>#include<map>using namespace std;#define LL long longconst int N = 110;const int INF = 0x3f3f3f3f;int a[N];int main(){    LL n,l,r;    while(cin>>n>>l>>r)    {        vector<int> bits;        while(n)        {            bits.push_back(n&1);            n /= 2;        }        reverse(bits.begin(),bits.end());        int sum = 0;        for(long long i = l; i <= r; i++)        {            int j = 0;            long long x = 1;            while(i%x == 0)            {                x <<= 1;                j++;            }            if(bits.size() <= j-1)                sum += 0;            else                sum += bits[j-1];            //PRintf("%d/n",sum);        }        printf("%d/n",sum);    }    return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表