# codeforces215 E. Periodical Numbers（数位dp）

E. Periodical Numbers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A non-empty string s is called binary, if it consists only of characters “0” and “1”. Let’s number the characters of binary string s from 1 to the string’s length and let’s denote the i-th character in string s as si.

Binary string s with length n is periodical, if there is an integer 1 ≤ k < n such that:

k is a divisor of number n
for all 1 ≤ i ≤ n - k, the following condition fulfills: si = si + k


For example, binary strings “101010” and “11” are periodical and “10” and “10010” are not.

A positive integer x is periodical, if its binary representation (without leading zeroes) is a periodic string.

Your task is to calculate, how many periodic numbers are in the interval from l to r (both ends are included).
Input

The single input line contains two integers l and r (1lr1018$1 ≤ l ≤ r ≤ 10^{18}$). The numbers are separated by a space.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Output

Print a single integer, showing how many periodic numbers are in the interval from l to r (both ends are included).
Sample test(s)
Input

1 10

Output

3

Input

25 38

Output

2

Note

In the first sample periodic numbers are 3, 7 and 10.

In the second sample periodic numbers are 31 and 36.

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=70;
int dig[maxn];
LL x,y;
LL dp[maxn];
LL cal(int len,int k,LL x)
{
LL a=0,b=0;
for(int i=0;i<k;i++)
a+=(dig[len-i]<<(k-1-i));
b=a;
for(int i=1;i<len/k;i++)
b<<=k,b+=a;
return a-(1<<(k-1))+1/*此处加1是因为全0的原因*/-(b>x);
}
LL solve(LL x)
{
int len=0;
LL n=x;
while(x)
{
dig[++len]=x%2;
x>>=1;
}
LL ans=0;
for(int i=2;i<=len;i++)
{
memset(dp,0,sizeof(dp));
for(int j=1;j<i;j++)
{
if(i%j)continue;
if(i<len)dp[j]=(1<<(j-1));
else dp[j]=cal(len,j,n);
for(int k=1;k<j;k++)
if(j%k==0)dp[j]-=dp[k];
ans+=dp[j];
}
}
return ans;
}
int main()
{
scanf("%I64d%I64d",&x,&y);
printf("%I64d\n",solve(y)-solve(x-1));
return 0;
}

• 本文已收录于以下专栏：

## CF_225B _Well-known Numbers

Numbers k-bonacci (k is integer, k > 1) are a generalization of Fibonacci numbers and are determined...
• rootial
• 2014年08月26日 17:41
• 287

## CodeForces - 225B题解

• Krone_
• 2017年08月26日 17:24
• 73

## codeforces 215E 数位DP

• haha593572013
• 2012年08月24日 22:49
• 1126

## CodeForces 215E Periodical Numbers 数位DP

• wangjie_wang
• 2013年08月29日 19:04
• 503

## codeforces #215 DIV1

A题读透题意就是查询区间x,y,z的个数，如果区间长度不超过2或者x,y,z个数的最大值与最小值的差不超过了就是YES，否则就是NO因为如果最大的差值小于等于2的话，无论如何都可以构造出一个序列使得每...
• yanglei040
• 2013年11月27日 10:25
• 566

## codeforces215E（数位DP，规律水过）

• gaokecs1
• 2014年02月07日 18:58
• 744

## HDOJ 2070 Fibbonacci Number

Fibbonacci Number Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other...
• shyazhut
• 2016年03月26日 21:35
• 236

## java170道面试题汇总+详细解析

• corleone_4ever
• 2017年10月15日 10:22
• 1443

## Magic Numbers codeforces 628D 数位dp

Consider the decimal presentation of an integer. Let’s call a number d-magic if digit d appears in d...
• Littlewhite520
• 2017年05月25日 01:08
• 221

## Round Numbers((组合数 + 简单组合数学)||(数位dp))

Round Numbers Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit St...