Codeforces 55D Beautiful Number (数位统计)
2013-06-17 21:54
337 查看
把数位dp写成记忆化搜索的形式,方法很赞,代码量少了很多。
下面为转载内容:
a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits.
问一个区间内[l,r]有多少个Beautiful数字
范围9*10^18
数位统计问题,构造状态也挺难的,我想不出,我的思维局限在用递推去初始化状态,而这里的状态定义也比较难
跟pre的具体数字有关
问了NotOnlySuccess的,豁然开朗 Orz
一个数字要被它的所有非零位整除,即被他们的LCM整除,可以存已有数字的Mask,但更好的方法是存它们的LCM{digit[i]}
int MOD = LCM{1,2,
View Code
下面为转载内容:
a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits.
问一个区间内[l,r]有多少个Beautiful数字
范围9*10^18
数位统计问题,构造状态也挺难的,我想不出,我的思维局限在用递推去初始化状态,而这里的状态定义也比较难
跟pre的具体数字有关
问了NotOnlySuccess的,豁然开朗 Orz
一个数字要被它的所有非零位整除,即被他们的LCM整除,可以存已有数字的Mask,但更好的方法是存它们的LCM{digit[i]}
int MOD = LCM{1,2,
const int MOD = 2520; LL dp[21][MOD][50]; int digit[21]; int indx[MOD+5]; void init() { int num = 0; for(int i = 1; i <= MOD; ++i) { if(MOD%i == 0) indx[i] = num++; } CL(dp, -1); } LL gcd(LL a, LL b) { return b == 0 ? a : gcd(b, a%b); } LL lcm(LL a, LL b) { return a/gcd(a, b)*b; } LL dfs(int pos, int presum, int prelcm, bool edge) { if(pos == -1) return presum%prelcm == 0; if(!edge && dp[pos][presum][indx[prelcm]] != -1) return dp[pos][presum][indx[prelcm]]; int ed = edge ? digit[pos] : 9; LL ans = 0; for(int i = 0; i <= ed; ++i) { int nowlcm = prelcm; int nowsum = (presum*10 + i)%MOD; if(i) nowlcm = lcm(prelcm, i); ans += dfs(pos - 1, nowsum, nowlcm, edge && i == ed); } if(!edge) dp[pos][presum][indx[prelcm]] = ans; return ans; } LL cal(LL x) { CL(digit, 0); int pos = 0; while(x) { digit[pos++] = x%10; x /= 10; } return dfs(pos - 1, 0, 1, 1); } int main() { //Read(); init(); int T; LL a, b; cin >> T; while(T--) { cin >> a >> b; cout << cal(b) - cal(a - 1) << endl; } return 0; }
View Code
相关文章推荐
- CodeForces 55D 数位统计 记忆化搜索
- Codeforces 204A Little Elephant and Interval(数位统计)
- codeforces 55D 求数字能被自己每个数位上的数整数的统计
- Codeforces 55D Beautiful Number (数位统计)
- codeforces 55D. Beautiful numbers(数位dp)
- codeforces 855E 数位DP
- CodeForces 215E 数位DP
- codeforces 908G - New Year and Original Order 数位dp
- hdu 4352 统计数字数位上最长上升子序列长度为k的个数
- codeforces 55D Beautiful numbers(数位dp)
- 【数位DP】 codeforces 55D && FZU chriswho
- [数位DP] Codeforces 809C Round #415 (Div. 1) C. Find a car
- CodeForces 55D Beautiful numbers (数位DP+状态简化,5级)
- CodeForces 258B Little Elephant and Elections 数位DP
- 【数位DP】 【CodeForces 55D】
- HDU 4389 X mod f(x)[数位统计dp]
- codeforces 55D 数位DP
- CodeForces 215E Periodical Numbers 数位DP
- [ACM] ural 1057 Amount of degrees (数位统计)
- CodeForces 1209 B. Jury Size 树状数组处理区间统计问题