CodeForces 55D Beautiful numbers(数位dp)
来源:互联网 发布:数字移相算法 编辑:程序博客网 时间:2024/06/06 02:58
题意:
求[l,r]区间内 能被其各位数字(除0外)整除的 数的个数
分析:
dp[i][cur][lcm]:=i位,当前数,数字的最小公倍数,很显然的状态
但是cur很大1018,我们知道lcm{1⋯9}=2520,可以用这个压缩cur,存个mod
对于lcm我们发现能整除lcm的数字不多,打表发现也就50个,离散化一下
dp[i][mod][lcmid]:=这样这样状态20∗2520∗50∗(10决策数)就可以通过了
PS:数位dp只用memset一次,状态可以重复利用−−之前T了一发
代码:
//// Created by TaoSama on 2015-10-19// Copyright (c) 2015 TaoSama. All rights reserved.////#pragma comment(linker, "/STACK:1024000000,1024000000")#include <algorithm>#include <cctype>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iomanip>#include <iostream>#include <map>#include <queue>#include <string>#include <set>#include <vector>using namespace std;#define pr(x) cout << #x << " = " << x << " "#define prln(x) cout << #x << " = " << x << endlconst int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;int __lcm(int a, int b) { return a / __gcd(a, b) * b;}typedef long long LL;//lcm 1...9 = 2520const int MAGIC = 2520;LL dp[25][MAGIC][50], digit[25];int mp[MAGIC]; //total 48LL dfs(int i, int mod, int lcm, int e) { if(!i) return mod % lcm == 0; if(!e && ~dp[i][mod][mp[lcm]]) return dp[i][mod][mp[lcm]]; LL ret = 0; int to = e ? digit[i] : 9; for(int d = 0; d <= to; ++d) { ret += dfs(i - 1, (mod * 10 + d) % MAGIC, d ? __lcm(lcm, d) : lcm, e && d == to); } if(!e) dp[i][mod][mp[lcm]] = ret; return ret;}LL calc(LL x) { int cnt = 0; for(; x; x /= 10) digit[++cnt] = x % 10; return dfs(cnt, 0, 1, 1);}int main() {#ifdef LOCAL freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin);// freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout);#endif ios_base::sync_with_stdio(0); int t; scanf("%d", &t); int cnt = 0; for(int i = 1; i <= MAGIC; ++i) if(MAGIC % i == 0) mp[i] = ++cnt; memset(dp, -1, sizeof dp); while(t--) { LL l, r; scanf("%I64d%I64d", &l, &r); printf("%I64d\n", calc(r) - calc(l - 1)); } return 0;}
0 0
- Codeforces 55D Beautiful numbers 数位dp
- Codeforces 55D Beautiful numbers --- 数位DP
- Codeforces 55D Beautiful numbers 数位dp
- 【codeforces】55D. Beautiful numbers 数位DP
- [CodeForces 55D] Beautiful numbers && 数位DP
- Codeforces 55D Beautiful numbers 数位DP
- 【数位DP】 Codeforces 55D Beautiful numbers
- codeforces 55D Beautiful numbers[数位dp]
- [Codeforces 55D]Beautiful numbers(数位DP)
- CodeForces 55D Beautiful numbers (数位DP)
- Codeforces 55D Beautiful numbers【数位dp】
- codeforces 55D. Beautiful numbers (数位dp)
- CodeForces 55D Beautiful numbers(数位dp)
- codeforces-55D-Beautiful numbers(数位DP)
- codeforces 55D Beautiful numbers 数位dp
- Codeforces 55D Beautiful numbers 数位DP
- CodeForces 55D Beautiful numbers 数位dp
- CodeForces 55D Beautiful numbers(数位dp)
- Oracle中对像名大小写敏感性的深入解析
- Cordys 不同 containner 利用 WebService 调用数据
- Java学习笔记——关于java中类中乱七八糟的概念的梳理
- (NO.00001)iOS游戏SpeedBoy Lite成形记(二)
- ACE_Service_Handler类的理解和使用
- CodeForces 55D Beautiful numbers(数位dp)
- 抱歉,戴尔,权威市场调研说公有云才是未来
- MongoDB Replica Sets + Sharding 方案 及 chunks块 和 片键分析
- 商务通通过JS获取sid、cid等参数
- oracle 数据更新讲解
- Java 多线程 并发编程
- MFC单文档视图之图标资源加载及绘制
- CaffeNet C++ Classification 例子运行方法
- WebSocket实例—初级聊天室(来自Tomcat8的examples)