【第22期】观点:IT 行业加班,到底有没有价值?

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

相关文章推荐

uva 10712 - Count the Numbers(数位dp)

题目链接:uva 10712 - Count the Numbers

CodeForces 215E Periodical Numbers 数位DP

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

程序员升职加薪指南!还缺一个“证”!

CSDN出品,立即查看!

CodeForces Round #112 Div2 165 E. Compatible Numbers

/* 经典DP 一定一个数组a1,a2,a3..... 对于每一个ai在此数组中找到一个数aj,使得ai&amp;aj=0,如果没有这样的aj,输出-1; n最大1000000. 对于一个ai,按位取反后的数x,肯定满足ai&amp;x=0;但aj=x是充分不必要条件,对与x,它的二...

Codeforces Round #215_div2_E. Sereja and the Arrangement of Numbers

转载注明出处  http://blog.csdn.net/moedane     传送门 http://codeforces.com/contest/368/problem/E   题意 ...

POJ 3340 Barbara Bennett'sWild Numbers(数位DP)

POJ 3340 Barbara Bennett'sWild Numbers(数位DP) http://poj.org/problem?id=3340 <p alig
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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