Description
给出一个区间 [L,R] ,问这个区间中有多少数字的二进制表示是有循环节的
Input
两个正整数 L,R (1≤L≤R≤1018)
Output
输出区间 [L,R] 中满足条件的数字个数
Sample Input
1 10
Sample Output
3
Solution
只需要求出
1
到
Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=66;
int res,a[maxn];
ll dp[maxn];
ll get(int len,int k,ll n)
{
ll x=0;
for(int i=0;i<k;i++)x=2ll*x+a[res-i-1];
ll y=0;
for(int i=0;i<len/k;i++)y=(y<<k)+x;
return x+1-(1<<(k-1))-(y>n);
}
ll Solve(ll n)
{
res=0;
ll nn=n;
while(nn)a[res++]=nn%2,nn/=2;
ll ans=0;
for(int i=2;i<=res;i++)
{
memset(dp,0,sizeof(dp));
for(int j=1;j<i;j++)
if(i%j==0)
{
if(i<res)dp[j]=1ll<<(j-1);
else dp[j]=get(i,j,n);
for(int k=1;k<j;k++)
if(j%k==0)dp[j]-=dp[k];
ans+=dp[j];
}
}
return ans;
}
int main()
{
ll L,R;
while(~scanf("%I64d%I64d",&L,&R))
printf("%I64d\n",Solve(R)-Solve(L-1));
return 0;
}