CF55D:Beautiful numbers(数位dp + 数论)
来源:互联网 发布:淘宝五张宝贝主图规律 编辑:程序博客网 时间:2024/06/08 19:59
reference:http://blog.csdn.net/lvshubao1314/article/details/43907225
题意:求区间内有多少个数,其本身能被各个位上的数字整除。
思路:略。
# include <stdio.h># include <string.h># define LL long long# define MOD 2520LL a[21], index[2521], dp[21][MOD][50];void init(){ int num = 0; for(int i=1; i<=MOD; ++i) if(!(MOD%i)) index[i] = num++;//最小公倍数离散化,优化内存。}int gcd(int a, int b){ if(b==0) return a; return gcd(b, a%b);}int lcm(int a, int b){ return a/gcd(a, b)*b;}LL dfs(int pos, int sum, int prelcm, bool limit){ if(pos == -1) return sum%prelcm==0; if(!limit && dp[pos][sum][index[prelcm]] != -1) return dp[pos][sum][index[prelcm]]; int up = limit?a[pos]:9; LL ans = 0; for(int i=0; i<=up; ++i) { int newsum = (sum*10+i)%MOD;//2520为1~9的最小公倍数,数值对2520取余,如果该数(比2520大)能被其各位数整除,则它一定能被2520整除。 int newlcm = prelcm; if(i) newlcm = lcm(newlcm, i); ans += dfs(pos-1, newsum, newlcm, limit&&(i==a[pos])); } if(!limit) dp[pos][sum][index[prelcm]] = ans; return ans;}LL solve(LL num){ int len = 0; while(num) { a[len++] = num%10; num /= 10; } return dfs(len-1, 0, 1, true);}int main(){ LL n, m; int t; init(); memset(dp, -1, sizeof(dp)); scanf("%d",&t); while(t--) { scanf("%I64d%I64d",&n,&m); printf("%I64d\n",solve(m)-solve(n-1)); } return 0;}
0 0
- CF55D:Beautiful numbers(数位dp + 数论)
- CF55D Beautiful numbers (数位DP)
- CodeForces 55D Beautiful numbers(数位dp+数论)
- codeforces55D. Beautiful numbers(数位dp+数论)
- Beautiful numbers (数位dp)
- 数位dp Beautiful numbers
- 数位dp, Beautiful numbers
- codeforces 55D D. Beautiful numbers(数位dp+数论)
- Codeforces 55D Beautiful numbers 数位dp 数论 优化技巧
- CF D. Beautiful numbers (数位dp)
- cf Beautiful numbers(数位dp)
- CF 55D - Beautiful numbers(数位DP)
- CodeForces 55D Beautiful numbers(数位dp)
- Codeforces Beta Round #51 D. Beautiful numbers (数位dp)
- CodeForces 55D Beautiful numbers (数位DP)
- codeforces 55D Beautiful numbers(数位dp进阶)
- Codeforces 55D - Beautiful numbers(数位dp)好
- Code Forces 55 D. Beautiful numbers(数位DP)
- Android官方资料--Configuration Examples
- 学习SQLite基础笔记
- Android权限Uri.parse的几种用法
- 深入研究容器
- 设计模式字典
- CF55D:Beautiful numbers(数位dp + 数论)
- [Boolan]第二周学习笔记——rico风
- 数据抓取学习(爬虫via scrapy)
- java多线程之继承Thread类创建线程类
- <algorithm>:next_permutation() HDU - 1027
- 数据结构学习四:树
- 蓝桥杯 2015 省赛 8 饮料换购
- 杭电1406 完数 java
- 我遇到过的 iOS 面试题(Object-C)