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 (1 ≤ l ≤ r ≤ 10181 ≤ 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.

思路:假如说现在处理的是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;
}

codeforces215 E. Periodical Numbers(数位dp)相关推荐

  1. CodeForces 215E Periodical Numbers 数位DP

    题意:给你一个区间[l,r],求这个区间内满足条件的数,条件是:这个数的二进制表示时,dig[i] == dig[i+k],(0<k<len,且len%k==0,len为这个数的二进制代码 ...

  2. CodeForces 215E Periodical Numbers 数位DP

    这题做了有好几天了,终于过了= = 完全不懂网上题解的递推写法,只能自己用记忆化搜索瞎搞,总算是搞出来了. 具体策略就是记忆化搜索的时候用一个tmp数组记录最前面len个的值,然后后面的数字必须要和前 ...

  3. codeforces215 E. Periodical Numbers(不一样的数位dp) 模板

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

  4. Educational Codeforces Round 8 D. Magic Numbers 数位DP

    D. Magic Numbers 题目连接: http://www.codeforces.com/contest/628/problem/D Description Consider the deci ...

  5. CodeForces - 628D Magic Numbers(数位dp)

    题目链接:点击查看 题目大意:首先规定一个不含前导零的数字如果满足: 从最高位开始,偶数位置全为 ddd 从最高位开始,奇数位置不能出现 ddd 则称该数字为 d−magicd-magicd−magi ...

  6. SPOJ - BALNUM Balanced Numbers(数位dp+进制转换)

    题目链接:点击查看 题目大意:给出平衡数的定义:每一个偶数出现的次数必须是奇数次,每一个奇数出现的次数必须是偶数次,求给定区间中有多少个平衡数 题目分析:数位dp,这个题目就难在怎么确定状态转移,本来 ...

  7. POJ - 3252 Round Numbers(数位dp)

    题目链接:点击查看 题目大意:规定一个数如果二进制中0的个数大于等于1的个数,则这个数称为"整数",问闭区间a,b中有多少个整数 题目分析:数位dp,dp[pos][c0][c1] ...

  8. Codeforces 55D Beautiful numbers (数位DP)

    题意:有T组询问,每次询问区间[l, r]中的beautiful number有多少.beautiful number是指这个数可以被组成它的数字整除.例如15是beautiful number,因为 ...

  9. codeforces 628D. Magic Numbers 数位dp

    题目链接 给两个数m, d. 两个数a, b. a, b长度小于2000, 长度相等.求在a, b之间的数x, x%m==0, 并且从高位往低位数, 奇数位的数全部不等于d, 偶数为的数全都等于d, ...

最新文章

  1. 20nian5yue最热门的Python开源项目
  2. android 插件化 模块化开发(apkplug)
  3. 第四百一十四节,python常用算法学习
  4. jQuery 文件上传插件:uploadify、swfupload
  5. 关于函数,对象以及闭包的一些理解
  6. vue-cli 打包部署
  7. 多媒体技术基础第四版林福宗pdf_意大利留学|没有绘画基础,还能考研艺术类专业吗?...
  8. django项目的创建与启动
  9. LeetCode 164. 最大间距
  10. 【英语学习】【WOTD】substantive 释义/词源/示例
  11. 虚拟机状态错误_【行业资讯】VirtualBox 6.1.16 发布,开源虚拟机
  12. 使用L2TPV3桥接---FR-TO-PPP
  13. box-shadow属性四个值_flex笔记1——flex-direction属性
  14. 《Java技术》预备作业总结
  15. C语言 爱心代码(颜色可改)
  16. 高中开计算机课吗,高中信息技术课的开设意义和课程实施方法
  17. php 判断浏览器和设备
  18. Dewplayer MP3网页播放器
  19. python 修改图片尺寸_用Python更改图片尺寸大小
  20. 学习Python的建议

热门文章

  1. PDF阅读器帮你解决文档字体显示难看的问题
  2. axios请求mysql_接收post请求(vue+axios)解决跨域问题(三)
  3. 计算机化验证方案,计算机化系统验证方案方针.doc
  4. 【MicroPython ESP32】1.8“tft ST7735带中文驱动显示示例
  5. C++volatile关键字
  6. 跨国并购捷径,中国企业欲在车联网领域弯道超车
  7. C51 - 存储器结构
  8. 多大的数据量称得上大数据
  9. 2022-2028全球相机液晶屏幕行业调研及趋势分析报告
  10. win11壁纸显示模糊怎么办 windows11壁纸显示模糊的解决方法