题目链接

题意:定义"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的更多相关文章

  1. 【数位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 ...

  2. CF 55D - Beautiful numbers(数位DP)

    题意: 如果一个数能被自己各个位的数字整除,那么它就叫 Beautiful numbers.求区间 [a,b] 中 Beautiful numbers 的个数. 分析:先分析出,2~9 的最大的最小公 ...

  3. CF 55D Beautiful numbers (数位DP)

    题意: 如果一个正整数能被其所有位上的数字整除,则称其为Beautiful number,问区间[L,R]共有多少个Beautiful number?(1<=L<=R<=9*1018 ...

  4. CF 55D. Beautiful numbers(数位DP)

    题目链接 这题,没想出来,根本没想到用最小公倍数来更新,一直想状态压缩,不过余数什么的根本存不下,看的von学长的blog,比着写了写,就是模版改改,不过状态转移构造不出,怎么着,都做不出来. #in ...

  5. 【数位DP】CF55D Beautiful numbers

    $dp[x][p][pp]$表示第x位,当前已有数字mod 2520(1~9数字的lcm)为p,当前各位数字的lcm为pp 观察到数组太大,考虑压缩,第三维lcm最多只有9个数字,打表发现最多只有48 ...

  6. CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)

    传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...

  7. Codeforces 55D. Beautiful numbers(数位DP,离散化)

    Codeforces 55D. Beautiful numbers 题意 求[L,R]区间内有多少个数满足:该数能被其每一位数字都整除(如12,24,15等). 思路 一开始以为是数位DP的水题,觉得 ...

  8. [Codeforces-div.1 55D] Beautiful numbers

    [Codeforces-div.1 55D] Beautiful numbers 试题分析 还是离散化...\(f_{i,j,k}\)表示i位,gcd为j,余数为k. #include<iost ...

  9. codeforces 55D - Beautiful numbers(数位DP+离散化)

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

随机推荐

  1. top命令

    TOP是一个动态显示过程,即可以通过用户按键来不断刷新当前状态.如果在前台执行该命令,它将独占前台,直到用户终止该程序为止.比较准确的说,top命令提供了实时的对系统处理器的状态监视.它将显示系统中C ...

  2. 【转】PHOTOSHOP常用快捷键大全

    PHOTOSHOP常用快捷键大全 一.文件新建 CTRL+N打开 CTRL+O 打开为 ALT+CTRL+O关闭 CTRL+W保存 CTRL+S 另存为 CTRL+SHIFT+S另存为网页格式 CTR ...

  3. Java简单示例-用户登录、单个页面的增删改查及简单分页

    index.html  -登录->stulist.jsp (index.html传递到LoginServlet,进行登录检测及写入session,NO返回index.html界面,OK 跳转到s ...

  4. python程序一直在后台运行的解决办法

    刚写了个python程序,要一直在后台运行,即使断开ssh进程也在,下面是解决办法: 假如Python程序为test.py 编写shell脚本start.sh #!/bin/bash python t ...

  5. G-FAQ – Why is Bit Depth Important?

    直接抄: https://apollomapping.com/2012/August/article15.html For this month’s Geospatial Frequently Ask ...

  6. webpack入坑之旅

    转自: http://guowenfh.github.io/2016/03/24/vue-webpack-01-base/ http://guowenfh.github.io/2016/03/25/v ...

  7. linux零基础入门总结

    啊,notepad++贴过来怎么对齐格式这么糟糕呢? #root用户 $普通用户   linux命令 清屏clear翻页清屏  CRT中ctrl+L    reset"清空 CRT中不起作用 ...

  8. JavaScript - 初识

    流程图分为顺序结构.选择结构.循环结构 引入JS的方式有1.使用外部的JS文件<script src="..."></script>2.使用标签直接嵌入网页 ...

  9. GenomicRangeQuery /codility/ preFix sums

    首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...

  10. Shell入门教程:Shell函数的返回值

    shell函数返回值一般有3种方式: 1.return语句(默认的返回值) shell函数的返回值可以和其他语言的返回值一样,通过return语句返回. 比如: #!/bin/bash functio ...