题目链接

题意:定义"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. 深度分析mysql GROUP BY 与 ORDER BY

    鉴于项目的需要,就从网上找到该文章,文章分析得很详细也很易懂,在android里,(不知道是不是现在水平的限制,总之我还没找到在用ContentProvider时可以使用子查询),主要方法是用SQLi ...

  2. 创建一个点状注记(MarkerElement)

    1.根据XY创建一个点 /// <summary> /// 根据x y创建新点 /// </summary> /// <param name="dX" ...

  3. GridView 动态添加绑定列和模板列

    动态添加绑定列很简单:例如: GridView1.DataSourceID = "SqlDataSource1"; BoundField bf1 = new BoundField( ...

  4. UIScrollView和控制器

    一般情况下,就设置UIScrollView所在的控制器 为 UIScrollView的delegate 设置控制器为UIScrollView的delegate有2种方法: 通过代码(self就是控制器 ...

  5. centos安装PHP服务器步骤

    方法一.使用网友开发的EZHTTP程序包一键安装. 可以参考地址http://www.centos.bz/2013/08/ezhttp-tutorial/ http://www.cnblogs.com ...

  6. sql 创建表、删除表 增加字段 删除字段操作

    下面是Sql Server 和 Access 操作数据库结构的常用Sql,希望对你有所帮助. 新建表:create table [表名]([自动编号字段] int IDENTITY (1,1) PRI ...

  7. json和pickle

    Pickle序列化 用于序列化的两个模块 json,用于字符串 和 python数据类型间进行转换 pickle,用于python特有的类型 和 python的数据类型间进行转换 python的pic ...

  8. 在MVC中实现文件的上传

    @using (Html.BeginForm("daoru", "Excel", FormMethod.Post, new { enctype = " ...

  9. twemproxy explore,redis和memcache代理服务器

    twemproxy,也叫nutcraker.是一个twtter开源的一个redis和memcache代理服务器. redis作为一个高效的缓存服务器,非常具有应用价值.但是当使用比较多的时候,就希望可 ...

  10. vmware esxi 找不到网卡驱动,硬盘的解决方法

    解决方法就是把ESXi无法识别的硬件的驱动定制进安装镜像文件中. ESXi 5.5 U2: VMware-VMvisor-Installer-5.5.0.update02-2068190.x86_64 ...