codeforces215 E. Periodical Numbers(数位dp)

原创 2015年07月07日 19:16:45

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). 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.

思路:假如说现在处理的是1-x,那么枚举所有可能的长度,然后枚举k 的大小,对于小于极限长度的长度,那么直接可以用1<<(j-1)算出,因为假如现在枚举的k为j,相当于处理最高位一样,剩下的可以随便变化;对于极限值,用cal计算

#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;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

CodeForces 215E Periodical Numbers 数位DP

题意:给你一个区间[l,r],求这个区间内满足条件的数,条件是:这个数的二进制表示时,dig[i] = dig[i+k] , (0 思路:考虑[0,x]这个区间,若x的位数为len,当数的长度 i 为...

codeforces215E(数位DP,规律水过)

地址:http://codeforces.com/contest/215/problem/E E. Periodical Numbers time limit pe...

codeforces 215E 数位DP

链接:http://www.codeforces.com/problemset/problem/215/E 题意:问你 l  r之间有多少的数是周期数,周期数的定义为:这个数的二进制形式是以k为周期...

【数位dp?】CodeForces 288E Polo the Penguin and Lucky Numbers

题目点这里 那么好吧终于把这道题折腾完了 以及我并不觉得这是dp = = 题意:所有数位仅含4和7的数为lucky number 设[L, R]中第i大的lucky number...

POJ 3252 E - Round Numbers(数位dp)(模板)

题目大意是:"Round Number "被称为其二进制形式中0的个数比1的个数多。求[x,y]区间内“Round Number”的个数。 这道题看起来难,其实理解了就简单了,就是一个二进制的数位dp...

CodeForces 55D A - Beautiful numbers(数位dp)(模板)

这几天在学数位dp,今天终于差不多弄清楚了数位dp的原理。 数位dp就是把一个数按位从最高位往前递归,按不同的状态来求个数,用dp存下求过的状态,下次就可以直接调用,不用再递归去找,这样就可以节省时间...

Educational Codeforces Round 8 D - Magic Numbers 数位DP

题意很简单,偶数位为d,奇数位不为d,且能被m整除的数,叫d-magic,给定d,m,a,b,求在[a,b]上d-magic的个数。 注意 1 既是 0-magic 又是 2/3/4/5/6/7/8...

Codeforces Beta Round #51---D. Beautiful numbers(数位dp, 巧妙)

Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer numb...

【codeforces】55D. Beautiful numbers 数位DP

传送门:【codeforces】55D. Beautiful numbers 题目分析:

codeforces 55D D. Beautiful numbers(数位dp+数论)

题目链接:codeforces 55D题目大意:求在[l,r]中能够整除自己每个数位上的数字的数的个数。题目分析: 首先我们能够知道如果这个数能够整除它的每个数位上的数字,那么它一定能够整除他们的最小...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)