CodeForces 55D Beautiful numbers (数位DP)
2015-07-22 15:27
309 查看
题意:一个数字,如果它能被所有的非零数字整除,就为 Beautiful numbers。
参考题解:点击打开链接
dp[位数][高位数字模2520的余数][最小公倍数]
AC代码:
参考题解:点击打开链接
dp[位数][高位数字模2520的余数][最小公倍数]
AC代码:
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> using namespace std; typedef __int64 LL; LL c[2550],g[2550][10],dp[20][2550][100],num[20],top; LL gcd(LL a,LL b){ return b ? gcd(b,a%b) : a; } void init(){ LL i,j,r; memset(dp,-1,sizeof(dp)); for(i = 1; i <= 2520;i++){ for(j = 0;j < 10;j++) g[i][j] = j ? i*j / gcd(i,j) : i; } for(i=1,r=0;i<=2520;i++){ if(2520 % i == 0) c[i] = r++; } } LL dfs(LL i,LL s,LL m,bool e){ if(i == -1) return s%m ? 0 : 1; if(!e && dp[i][s][c[m]] != -1) return dp[i][s][c[m]]; LL res = 0; LL u = e ? num[i] : 9 , d; for(d = 0 ; d <= u ; d++){ res += dfs(i-1,(s*10+d)%2520,g[m][d],e && d == u); } return e ? res : dp[i][s][c[m]] = res; } LL get(LL n){ top = 0; for(;n;n/=10) num[top++] = n%10; return dfs(top-1,0,1,1); } int main(){ init(); LL cas,l,r; scanf("%I64d",&cas); while(cas--){ scanf("%I64d%I64d",&l,&r); printf("%I64d\n",get(r) - get(l-1)); } }
相关文章推荐
- spring事物管理
- velocity第9个应用例子 ---格式化日期
- OS中atomic的实现解析
- Hybirs 使用impex删除一个表的所有内容
- “工业4.0”
- 2012年5月SAT香港真题解析
- 让dijit DateTextBox不可手动输入
- poj 2031 Building a Space Station(最小生成树)
- 杀死进入死循环的进程
- velocity第8个应用例子----#if#else#end
- st算法 求区间最值问题
- poj 2031 Building a Space Station(最小生成树)
- UVA 1663 Purifying Machine (二分图匹配,最大流)
- working set的使用
- 众美窝窝徐茂栋:O2O企业将面临倒闭潮
- 获取手机联系人信息
- POJ 3268
- CentOS搭建nginx+tomcat+laravel+php+Mongodb环境1(nginx篇)
- JS实现简单移动动画函数
- Ubuntu 安装svn服务器