Codeforces 55D Beautiful numbers (数位dp,状压辅助)
2014-12-12 11:10
537 查看
题意:
给出一个范围[L,R] 问这个范围内满足这样条件的数的个数:这个数能整出其各个位上的数。
题解:
开始没思路,但是dp的题目思考都是从状态入手,用钟亮的一句话要什么状态就加什么状态呗。那么就单单要判断一个数是否满足至少要用到:位数、这个数本身、各个位上的数。
第一个好说。
第二个,因为数可以很大,数组下标存不了,其实可以用同余定理,如果要被各个位上的数整数,那么这个数对各个位上的数的最小公倍数取模后性质不变,计算的1-9的最小公倍数2520那么就可以把这个数本身控制在2520范围内。
第三个,各个位上的数,离散的,我们用而进制压缩来搞定,把出现的数状压,这样最后只要判断是否能整数这个状态里的数就好了。
体会:
给出一个范围[L,R] 问这个范围内满足这样条件的数的个数:这个数能整出其各个位上的数。
题解:
开始没思路,但是dp的题目思考都是从状态入手,用钟亮的一句话要什么状态就加什么状态呗。那么就单单要判断一个数是否满足至少要用到:位数、这个数本身、各个位上的数。
第一个好说。
第二个,因为数可以很大,数组下标存不了,其实可以用同余定理,如果要被各个位上的数整数,那么这个数对各个位上的数的最小公倍数取模后性质不变,计算的1-9的最小公倍数2520那么就可以把这个数本身控制在2520范围内。
第三个,各个位上的数,离散的,我们用而进制压缩来搞定,把出现的数状压,这样最后只要判断是否能整数这个状态里的数就好了。
体会:
这道题的精髓:一个数能被它的所有非零数位整除,则能被它们的最小公倍数整除
#include<iostream> #include<math.h> #include<stdio.h> #include<algorithm> #include<string.h> #include<vector> #include<map> using namespace std; typedef long long lld; const int oo=0x3f3f3f3f; const lld OO=1LL<<61; const int MOD=2520; #define eps 1e-6 const int maxn=105; lld dp[22][1<<8][2525]; int bit[22]; int ok(int st,int num) { for(int i=2;i<=9;i++) { if(st&(1<<(i-2))) if(num%i) return 0; } return 1; } lld dfs(int pos,int st,int num,int f) { if(pos<1) return ok(st,num); if(!f&&dp[pos][st][num]!=-1) return dp[pos][st][num]; lld res=0; int last=f?bit[pos]:9; for(int i=0;i<=last;i++) { int next=st; if(i>=2) next|=(1<<(i-2)); res+=dfs(pos-1,next,(num*10+i)%MOD,f&&i==last); } if(!f)dp[pos][st][num]=res; return res; } lld Cnt(lld a) { int len=0; while(a) { bit[++len]=a%10; a/=10; } return dfs(len,0,0,1); } int main() { memset(dp,-1,sizeof dp); int T; lld a,b; scanf("%d",&T); while(T--) { cin>>a>>b; cout<<Cnt(b)-Cnt(a-1)<<endl; } return 0; } /** 2 1 9 12 15 */
相关文章推荐
- codeforces 55D. Beautiful numbers(数位dp)
- beautiful number 数位DP codeforces 55D
- Codeforces 628D 偶数位全是某个数 数位dp
- codeforces 855E 数位DP
- codeforces--507D--The Maths Lecture(数位dp)
- 专题(弱点)Dp训练总结【状压Dp*1+区间Dp*5+数位dp*3+树型Dp*2】【10/11】
- codeforces 204A Little Elephant and Interval (数位dp)
- CodeForces 417D Cunning Gena 状压dp
- CodeForces 204A Little Elephant and Interval 数位DP
- CodeForces - 55D——Beautiful numbers (数位dp)
- Codeforces 678E Another Sith Tournament 状压DP
- CodeForces 55D Beautiful numbers(数位dp)
- codeforces 55D Beautiful numbers(数位DP)
- 【数位dp && 优化数组范围】CodeForces - 55D Beautiful numbers
- Codeforces 757 D. Felicity's Big Secret Revealed 状压DP
- 【数位DP】 【CodeForces 55D】
- codeforces 385d bear and floodlight【状压DP之暴力枚举】
- CodeForces 55D 数位DP 能被它自身数位上的所有数整除
- Light OJ 1158 Anagram Division(状压+数位DP)
- SPOJ BALNUM Balanced Numbers 状压+数位DP