Codeforces 258B 数位DP
2013-03-17 19:54
316 查看
递归
非递归
#include <cstdio> #include <cstring> #include <algorithm> #define LL __int64 const LL mod = 1000000007; LL dp[11], ans; int len; char a[11]; void add(int dep, int dig, int num) { if(!dep) { dp[dig] += num; return; } add(dep-1, dig+1, num*2); add(dep-1, dig, num*8); } void dfs(int dep, int sum, int tot) { int i; if(sum >= len) return; if(dep == 6) { for(i = sum+1; i <= len; i++) ans = (ans + tot*dp[i])% mod; return; } for(i = 0; i < len; i++) { if(dp[i]) { dp[i]--; dfs(dep+1, sum+i, tot*(dp[i]+1)%mod); dp[i]++; } } } int main() { int i, j, t = 0; len = strlen(gets(a)); for(i = 0; i < len; i++) { for(j = 0; j < a[i] - '0'; j++) { if(j == 4 || j == 7) add(len-1-i, t+1, 1); else add(len-1-i, t, 1); } if(a[i] == '4' || a[i] == '7') t++; } dp[t]++, dp[0]--; // for(i = 0; i <= 9; i++) // printf("%d\n", dp[i]); dfs(0, 0, 1); printf("%I64d\n", ans); return 0; }
非递归
#include <cstdio> #include <cstring> #include <iostream> //#include <algorithm> using namespace std; #define LL __int64 const int mod = (int)1e9+7; int n; LL f[11]; LL dp[11][11]; void add(LL &a, LL b) { a += b; if(a >= mod) a -= mod; } void init() { int i, j; dp[1][0] = 8; dp[1][1] = 2; for(i = 2; i <= 10; i++) for(j = 0; j <= i; j++) { if(j) add(dp[i][j], dp[i-1][j-1]*2); add(dp[i][j], dp[i-1][j]*8); } } int a[11], len; void gao(int n) { len = 0; while(n) { a[len++] = n % 10; n/=10;} int i, j, k; int cnt = 0; for(i = len-1; i >= 0; i--) { for(j = 0; j < a[i]; j++) { int c = (j==4||j==7) ? 1 : 0; for(k = 0; k <= 11; k++) { if(cnt+c+k >= 10) break; add(f[cnt+c+k], dp[i][k]); } if(!i) add(f[cnt+c], 1); // pay attention!!!! } if(a[i]==4||a[i]==7) cnt++; } f[cnt]++; f[0]--; } LL ans; void search(int dep, int sum, LL cnt) { int i; if(sum >= len) return; if(dep == 6) { for(i = sum+1; i <= len; i++) ans = (ans + cnt*f[i]) % mod; return; } for(i = 0; i < len; i++) if(f[i]) { f[i]--; search(dep+1, sum+i, cnt*(f[i]+1)%mod); f[i]++; } } int main() { cin >> n; init(); gao(n); search(0, 0, 1); printf("%I64d\n", ans); return 0; }
相关文章推荐
- CodeForces 258B Little Elephant and Elections 数位DP
- codeforces 258B Little Elephant and Elections 数位DP+DFS
- 【数位dp】【HDU 3555】【HDU 2089】【UESTC 1307】【CodeForces 258B】数位DP入门题
- CodeForces 55D Beautiful numbers(数位dp+数论)
- 【数位DP】 【CodeForces 55D】
- 【codeforces】55D. Beautiful numbers 数位DP
- Codeforces 653B Bear and Compressing 【数位dp】
- codeforces-55D-Beautiful numbers(数位DP)
- Codeforces Good bye 2015 B. New Year and Old Property dfs 数位DP
- codeforces 401D Roman and Numbers (数位dp)
- [Codeforces 55D]Beautiful numbers(数位DP)
- codeforces 628D 数位dp
- Codeforces-55D Beautiful numbers (数位DP)
- CodeForces 55D Beautiful numbers(数位dp)
- CodeForces 54C-First Digit Law(数位,概率dp)
- CodeForces 55D. Beautiful numbers (思维+数位DP)
- codeforces--507D--The Maths Lecture(数位dp)
- Codeforces 55D - Beautiful numbers(数位dp)好
- codeforces 55D - Beautiful numbers 数位DP+DFS
- CodeForces 55D 数位DP 能被它自身数位上的所有数整除