UVALive 4864 数位dp
2013-05-08 17:03
113 查看
Start with an integer, N0, which is greater than 0. Let N1 be the number of ones in the binary representation of N0. So, if N0 = 27, N1 = 4. For all i > 0, let Ni be the number of ones in the binary representation of Ni-1. This sequence will always converge to one. For any starting number, N0, let K be the minimum value of i 0 for which Ni = 1. For example, if N0 = 31, then N1 = 5, N2 = 2, N3 = 1, so K = 3.
Given a range of consecutive numbers, and a value X, how many numbers in the range have a K value equal to X?
64位同时为1时即转换为了64以下的数转换到1的步数,所以首先打个表,之后枚举数位之和就ok了。
View Code
Given a range of consecutive numbers, and a value X, how many numbers in the range have a K value equal to X?
64位同时为1时即转换为了64以下的数转换到1的步数,所以首先打个表,之后枚举数位之和就ok了。
View Code
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> using std::cout; using std::cin; using std::endl; typedef long long LL; int const N = 64; int step ,bit[N+10],ln; LL lo,hi; int x; LL dp [2]; int getsum1(int n) { int sum=0; int num=n; for(;n;sum+=n%2,n>>=1); if(sum==1)return 1; if(step[sum])return step[sum]+1; step[num]=getsum1(sum)+1; } void pre() { step[1]=0; for(int i=2;i<=64;i++) { if(!step[i]) step[i]=getsum1(i); } } LL getsum2(int t,int pre,int rest,int limit) { if(rest<0)return 0; if(!t)return (rest==0); int up=(limit?bit[t]:1); LL ans=0; if(!limit&&dp[t][rest][pre]!=-1)return dp[t][rest][pre]; for(int i=0;i<=up;i++) { ans+=getsum2(t-1,i,rest-i,limit&&i==up); } if(!limit&&dp[t][rest][pre]==-1)dp[t][rest][pre]=ans; return ans; } LL getsum3(LL n) { if(n<=0)return 0; for(ln=0;n;bit[++ln]=n%2,n>>=1); LL ans=0; if(x==0) return 1; if(x==step[1]+1) ans+=getsum2(ln,0,1,1)-1; for(int i=2;i<=ln;i++) if(x==step[i]+1) ans+=getsum2(ln,0,i,1); return ans; } int main() { pre(); memset(dp,-1,sizeof(dp)); while(scanf("%lld %lld",&lo,&hi)) { scanf("%d",&x); if(!(x+lo+hi))break; printf("%lld\n",getsum3(hi)-getsum3(lo-1)); } return 0; }
相关文章推荐
- UVALive 4864 Bit Counting --记忆化搜索 / 数位DP?
- [UVALive4864] Bit Counting && 数位DP
- UVALive - 6872 Restaurant Ratings 数位dp
- [UVALive3675]Sorted bit sequence && 数位DP
- uvalive7271(A Math Problem) 数位dp
- UVALive 7271 A Math Problem 【数位dp计数】
- UVALive 3782 Bigger is Better(数位DP + 大数)
- UVALive 5004 Balanced Number && hdu-3967 Zero's Number(数位dp)
- UVALive 3675 Sorted bit sequence(数位dp+二分)
- UVA 11361 - Investigating Div-Sum Property 数位DP
- UVALive 3907 (LA 3907) Puzzle AC自动机 + 搜索DP 记录路径
- UVALive - 3942 Remember the Word(字典树+dp)
- UVA 12105 Bigger is Better(数位dp)
- UVALive 7003 A Balance Game on Trees(树形dp)
- uva live 3516 Exploring Pyramids 区间DP
- UVALive 2221 Frontier(计算几何 + DP)
- UvaLive 6531 Go up the ultras DP+RMQ
- UVALive 3942 Remember the Word(trie + dp)
- UVA 12486 Space Elevator(数位DP)
- UVALive 4998 浅谈欧拉定理优化数位动态规划求解高阶同余方程