beautiful number(HDU 5179 数位DP)
来源:互联网 发布:热传导计算软件 编辑:程序博客网 时间:2024/05/21 05:20
beautiful number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 883 Accepted Submission(s): 571
Problem Description
Let A=∑ni=1ai∗10n−i(1≤ai≤9) (n is the number of A 's digits). We call A as “beautiful number” if and only if a[i]≥a[i+1] when 1≤i<n and a[i] mod a[j]=0 when 1≤i≤n,i<j≤n (Such as 931 is a "beautiful number" while 87 isn't).
Could you tell me the number of “beautiful number” in the interval[L,R] (including L and R)?
Could you tell me the number of “beautiful number” in the interval
Input
The fist line contains a single integer T (about 100), indicating the number of cases.
Each test case begins with two integersL,R(1≤L≤R≤109) .
Each test case begins with two integers
Output
For each case, output an integer means the number of “beautiful number”.
Sample Input
21 11999999993 999999999
Sample Output
102
//题意:
给一个数A,a[i]表示它 i 位上的数字,若a[i]>=a[i+1] (1<=i<n)且a[i] % a[j]==0(1<=i<j<=n),我们称数A为beautiful number。现在给你L,R,求[L,R]上beautiful number的个数。
//思路:
简单的数位DP+记忆化搜索:
定义一个二位DP数组:dp[i][j] : i 表示数位,j 表示该位前一位数字的值。
设第 i 位的值为num,它前一位的值为front,那么若满足front%num==0说明到目前为止是可行的,否则的话有这个前缀的所有数都不满足要求。(如果front<num,那么front%num的值必不可能==0);
此外,只有首位能取0,即若该数位前面的数位有非0值,那么此位及它后面的数位的值一定不能取0。
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <cmath>#include <vector>#include <algorithm>using namespace std;int cnt;int bit[20];int dp[20][20];int dfs(int pos, int front, int mod, int flag){if (pos <= 0)return (mod == 0);if (!flag&&dp[pos][front] != -1)return dp[pos][front];int num = flag ? bit[pos] : 9;int ans = 0;for (int i = 0; i <= num; i++){if (i == 0 && pos < cnt&&front != 0)continue;else if (pos == cnt || front == 0){//0不能取if (i == 0 && pos == 1){mod = 1;continue;}elsemod = 0;}else{if (front%i == 0)mod = 0;else{mod = 1;continue;}}ans += dfs(pos - 1, i, mod, flag&&i == num);}if (!flag)dp[pos][front] = ans;return ans;}int solve(int num){cnt = 0;memset(bit, 0, sizeof(bit));while (num){bit[++cnt] = num % 10;num = num / 10;}int ans;if (cnt == 0)ans = 0;elseans = dfs(cnt, 10, 0, 1);return ans;}int main(){int T;scanf("%d", &T);memset(dp, -1, sizeof(dp));while (T--){int l, r;scanf("%d%d", &l, &r); int ans1 = solve(r);int ans2 = solve(l - 1);printf("%d\n", ans1 - ans2);}return 0;}
阅读全文
0 0
- hdu 5179 beautiful number (数位dp)
- hdu 5179 beautiful number【数位dp】
- HDU 5179 beautiful number 数位DP(水)
- beautiful number(HDU 5179 数位DP)
- hdu 5179 beautiful number (数位DP)
- hdu 5179 beautiful number(打表,数位DP)
- (hdu 5179)beautiful number(数位DP)
- HDU5179 beautiful number 数位DP
- FZU 2070 - Beautiful number 数位DP..
- CF-55D-数位DP-Beautiful Number
- HDU5179 beautiful number(数位DP)
- 【CodeForces 55D】Beautiful number 数位DP
- hdu(5179):beautiful number
- hdu 5179-beautiful number
- 【数位DP】B-number HDU
- BC#31.1002beautiful number——数位DP/暴力
- Codeforces 55D Beautiful Number (数位dp)
- CF 55D beautiful number(数位dp)
- 微信小程序公布六个新能力,向个人开发者开放注册
- ssh学习
- 欢迎使用CSDN-markdown编辑器
- MATLAB 提示没有fhog或hog函数时需要添加piotr_toolbox工具包——附下载
- spring aop学习5:jdk静态代理
- beautiful number(HDU 5179 数位DP)
- 微软Docs.com会泄露隐私文件,搜索功能已关闭
- 小米6配置全曝光,或搭载索尼XZ Premium同款摄像传感器
- Youtube自动字幕系统已能识别环境音,但国内还在依靠字幕组
- longest-common-prefix
- VS2015中用gets_s读取EOF
- JZOJ 5429 排列
- python中'zip' is not subscriptable
- Reading Note: Detect to Track and Track to Detect