数位DP CF 55D Beautiful numbers
题意:定义"beautiful number"为一个数n能整除所有数位上非0的数字
分析:即n是数位所有数字的最小公倍数的倍数。LCM(1到9)=2520。n满足是2520的约数的倍数。dp[len][val][lcm]一维为数的位数,一维为%2520的值(保存原数不可能,也没必要,2520是可行的最小公倍数最大的一个),一维为当前数位的lcm,判断满足的条件是val%lcm==0。这题离散化2520的约数,否则空间开不下。
#include <bits/stdc++.h> typedef long long ll;
ll dp[20][2520][50];
int digit[20];
int id[2521];
int tot; int GCD(int a, int b) {
return b ? GCD (b, a % b) : a;
}
int LCM(int a, int b) {
return a / GCD (a, b) * b;
} void init_LCM() {
tot = 0;
for (int i=1; i<=2520; ++i) {
if (2520 % i == 0) {
id[i] = ++tot;
}
}
} ll DFS(int pos, int val, int lcm, bool limit) {
if (pos == -1) {
return val % lcm == 0;
}
ll &now = dp[pos][val][id[lcm]];
if (!limit && now != -1) {
return now;
}
int d = limit ? digit[pos] : 9;
ll ret = 0;
for (int i=0; i<=d; ++i) {
int tval = (val * 10 + i) % 2520;
int tlcm = i ? lcm / GCD (lcm, i) * i : lcm;
ret += DFS (pos - 1, tval, tlcm, limit && i == d);
}
if (!limit) {
now = ret;
}
return ret;
} ll solve(ll x) {
int len = 0;
if (x == 0) {
digit[len++] = 0;
} else {
while (x) {
digit[len++] = x % 10;
x /= 10;
}
}
return DFS (len - 1, 0, 1, true);
} int main() {
init_LCM ();
memset (dp, -1, sizeof (dp));
int T; scanf ("%d", &T);
while (T--) {
ll l, r; std::cin >> l >> r;
std::cout << solve (r) - solve (l - 1) << '\n';
}
return 0;
}
数位DP CF 55D Beautiful numbers的更多相关文章
- 【数位dp】CF 55D Beautiful numbers
题目 Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer n ...
- CF 55D - Beautiful numbers(数位DP)
题意: 如果一个数能被自己各个位的数字整除,那么它就叫 Beautiful numbers.求区间 [a,b] 中 Beautiful numbers 的个数. 分析:先分析出,2~9 的最大的最小公 ...
- CF 55D Beautiful numbers (数位DP)
题意: 如果一个正整数能被其所有位上的数字整除,则称其为Beautiful number,问区间[L,R]共有多少个Beautiful number?(1<=L<=R<=9*1018 ...
- CF 55D. Beautiful numbers(数位DP)
题目链接 这题,没想出来,根本没想到用最小公倍数来更新,一直想状态压缩,不过余数什么的根本存不下,看的von学长的blog,比着写了写,就是模版改改,不过状态转移构造不出,怎么着,都做不出来. #in ...
- 【数位DP】CF55D Beautiful numbers
$dp[x][p][pp]$表示第x位,当前已有数字mod 2520(1~9数字的lcm)为p,当前各位数字的lcm为pp 观察到数组太大,考虑压缩,第三维lcm最多只有9个数字,打表发现最多只有48 ...
- CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)
传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...
- Codeforces 55D. Beautiful numbers(数位DP,离散化)
Codeforces 55D. Beautiful numbers 题意 求[L,R]区间内有多少个数满足:该数能被其每一位数字都整除(如12,24,15等). 思路 一开始以为是数位DP的水题,觉得 ...
- [Codeforces-div.1 55D] Beautiful numbers
[Codeforces-div.1 55D] Beautiful numbers 试题分析 还是离散化...\(f_{i,j,k}\)表示i位,gcd为j,余数为k. #include<iost ...
- codeforces 55D - Beautiful numbers(数位DP+离散化)
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
随机推荐
- Startup配置类 居然又是约定
Microsoft.Owin.Host.SystemWeb 这个dll可以让OWin接管IIS的请求,虽然同样是托管在IIS,但是所有的请求都会被OWin来处理.在OWin的4层结构中(Applica ...
- windows安装mysql5.7
1下载mysql http://dev.mysql.com/downloads/mysql/2解压后,新建一个data文件夹,复制my-default.ini,并改名为my.ini,添加下面内容[cl ...
- 使用Spring和SpringMVC管理bean时要注意的一个小细节
最近一直在做毕业设计...用到了Shiro和SpringMVC..用过shiro的朋友都知道shiro需要自己去写Realm,然后把Realm注入到SecurityManager中.而Security ...
- Error: Collection was modified; enumeration operation may not execute.
http://blog.csdn.net/ffeiffei/article/details/6131254
- Android 网络编程
HttpClient 发送get请求 创建一个客户端对象 HttpClient client = new DefaultHttpClient(); 创建一个get请求对象 HttpGet hg = n ...
- java表格操作之设置表格列宽
设置所有列的宽度 /** * 设置所有列的列宽 * @param table * @param width */ public void setAllColumnWidth(JTable table, ...
- mysql 主从master-slave同步复制 配置,为读写分离准备
1.为方便,我在一个windows下安装两个mysql实例,端口分别是 3306.3307 打开 my.ini 或 my-default.ini 文件 配置 basedir datadir 和port ...
- UI第九节——UIStepper
- (void)viewDidLoad { [super viewDidLoad]; // 实例化UIStepper,大小是固定的 UIStepper *stepper = ...
- 获取PHP文件绝对地址$_SERVER['SCRIPT_FILENAME'] 与 __FILE__ 的区别
通常情况下,PHP $_SERVER['SCRIPT_FILENAME'] 与 __FILE__ 都会返回 PHP 文件的完整路径(绝对路径)与文件名: <?php echo 'SCRIPT_F ...
- Alpha版本十天冲刺——Day 5
站立式会议 会前小侃:今天是双11,也是恰逢组内秋鑫同学生日,本组同学祝他双11生日快乐.天气好冷,注意保暖. 会议总结 队员 今天完成 遇到的问题 明天要做 感想 鲍亮 json数据解析学习,完成注 ...