CF 258B Little Elephant and Elections
2014-03-01 20:48
489 查看
数位dp。 先找出含不同数目幸运数字的数量。然后直接暴力求解。
本题是第一次做这种找不同数目特定数字的数位dp,加一个参量。在函数外面加一重循环,指定数目,最后判断是否与预期数目相同即可。
暴力求种类数每一个政党可能选的所有数目是相加的,每一种数目的下的种类与后面政党的所有可能数相乘。一开始全部相乘搞错了。波神是dp过的,10^5,优化了两个数量级。感觉没有太大必要
提交时db了两次,第一是交错题,然后INF是1000000007,我令为1000000009了。。。
本题是第一次做这种找不同数目特定数字的数位dp,加一个参量。在函数外面加一重循环,指定数目,最后判断是否与预期数目相同即可。
暴力求种类数每一个政党可能选的所有数目是相加的,每一种数目的下的种类与后面政党的所有可能数相乘。一开始全部相乘搞错了。波神是dp过的,10^5,优化了两个数量级。感觉没有太大必要
提交时db了两次,第一是交错题,然后INF是1000000007,我令为1000000009了。。。
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> using namespace std; #define INF 1000000007 #define LL long long char a[20]; LL sum[20]; LL dp[20][20]; int len; LL dfs(int id, int s, bool d, int aim){ if(s > aim) return 0; if(id >= len) return s == aim; if(dp[id][s] != -1 && !d)return dp[id][s]; int edge = d ? a[id] - '0' : 9; LL ans = 0; for(int i = 0; i <= edge; i++){ if(i == 4 || i == 7) ans += dfs(id + 1, s + 1, d && i == edge, aim); else ans += dfs(id + 1, s , d && i == edge, aim); } return d ? ans : dp[id][s] = ans; } LL dfs2(int id, int num, int sn){ if(id >= 6)return num < sn ? 1 : 0; LL ans = 0; for(int i = 0; i < sn - num; i++){ if(sum[i] > 0){ sum[i] --; ans += (sum[i] + 1) * dfs2(id + 1, num + i, sn) % INF; ans %= INF; sum[i] ++; } } return ans; } int main(){ scanf("%s", a); len = strlen(a); memset(sum, 0, sizeof(sum)); for(int i = 0; i <= 9; i++){ memset(dp, -1, sizeof(dp)); sum[i] = dfs(0, 0, true, i); if(i == 0)sum[0]--; }//printf("\n"); LL ans = 0; for(int i = 0; i <= 9; i++){ if(sum[i] > 0){ ans += sum[i] * dfs2(0, 0, i) % INF; ans %= INF; } } printf("%I64d\n", ans); return 0; }
相关文章推荐
- Codeforces 258B - Little Elephant and Elections
- 数位dp + dfs + 逆元求余 CF 258 B. Little Elephant and Elections
- Little Elephant and Inversions CF220E
- CF 220B Little Elephant and Array
- Codeforces Round #157 (Div. 2) D. Little Elephant and Elections(数位DP+枚举)
- CF 205 CLittle Elephant and Interval 分类讨论(计数) D(暴力,水题)
- Little Elephant and Elections小象选举
- Codeforces Round #157 (Div. 1) B. Little Elephant and Elections
- CodeForces 258 B.Little Elephant and Elections(数位DP+dfs)
- CF 220C Little Elephant and Shifts
- CF Little Elephant and Shifts
- CF 205 C Little Elephant and Interval(模拟)
- 【CF】220B Little Elephant and Array
- Little Elephant and Bits
- CodeForces 258B Little Elephant and Elections 数位DP
- A. Little Elephant and Chess
- Codeforces Round #129 (Div. 2) B. Little Elephant and Sorting
- CF 454A(Little Pony and Crystal Mine-画图[水])
- codeforces 258-E. Little Elephant and Tree 线段树,离线
- *[topcoder]LittleElephantAndBalls