链接:

https://vjudge.net/problem/CodeForces-55D

题意:

Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue with this and just count the quantity of beautiful numbers in given ranges.

思路:

数位DP,但是数的范围太大不能直接记录,先优化。

我们有可以推出\(sum \% (n*x) \% x = sum \% x\)

证明如下:

令 \(sum = k*x+b\)

\((k * x+b)\% (n * x) \% x\)

令 \(k = k_a*n+k_b\)

\(((k_a*n+k_b)*x+b) \% (n*x) \% x\)

\((k_a*n*x+k_b*x+b) \% (n*x) \% x\)

\((k_b*x+b) \% x\)

\(b = (k*x+b) \% x = b\)

所以我们可以把值先模nx,取nx = 2520(1~9的lcm),

令Dp(i, j, k),表示i位置,模 n*x为j,k等于各位的lcm,(因为n%每个数都为0 = n%lcm = 0)

考虑lcm的个数,如果用2520来记录会爆内存,考虑lcm的值,每次都是两个数相乘/gcd,同时每个最多为9,则每个lcm都是2520的约数,枚举约数离散化。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MOD = 1e9+7;
const int MAXN = 1e6+10; LL Dp[30][2600][50];
int Hash[2600];
int dig[30]; LL Gcd(LL a, LL b)
{
if (b == 0)
return a;
return Gcd(b, a%b);
} LL Dfs(int pos, LL num, int lcm, bool lim)
{
if (pos == -1)
return num%lcm == 0;
if (!lim && Dp[pos][num][Hash[lcm]] != -1)
return Dp[pos][num][Hash[lcm]];
int up = lim ? dig[pos] : 9;
LL cnt = 0;
for (int i = 0;i <= up;i++)
cnt += Dfs(pos-1, (num*10+i)%2520, i ? lcm*i/Gcd(lcm, i) : lcm, lim && i == up);
if (!lim)
Dp[pos][num][Hash[lcm]] = cnt;
return cnt;
} LL Solve(LL x)
{
int p = 0;
while(x)
{
dig[p++] = x%10;
x /= 10;
}
return Dfs(p-1, 0, 1, true);
} int main()
{
// freopen("test.in", "r", stdin);
int cnt = 0;
for (int i = 1;i <= 2520;i++)
{
if (2520%i == 0)
Hash[i] = ++cnt;
}
memset(Dp, -1, sizeof(Dp));
int t;
scanf("%d", &t);
while(t--)
{
LL a, b;
scanf("%I64d %I64d", &a, &b);
printf("%I64d\n", Solve(b)-Solve(a-1));
} return 0;
}

CodeForces - 55D - Beautiful numbers(数位DP,离散化)的更多相关文章

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

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

  2. CodeForces - 55D Beautiful numbers —— 数位DP

    题目链接:https://vjudge.net/problem/CodeForces-55D D. Beautiful numbers time limit per test 4 seconds me ...

  3. Codeforces - 55D Beautiful numbers (数位dp+数论)

    题意:求[L,R](1<=L<=R<=9e18)区间中所有能被自己数位上的非零数整除的数的个数 分析:丛数据量可以分析出是用数位dp求解,区间个数可以转化为sum(R)-sum(L- ...

  4. codeforces 55D. Beautiful numbers 数位dp

    题目链接 一个数, 他的所有位上的数都可以被这个数整除, 求出范围内满足条件的数的个数. dp[i][j][k], i表示第i位, j表示前几位的lcm是几, k表示这个数mod2520, 2520是 ...

  5. FZU2179/Codeforces 55D beautiful number 数位DP

    题目大意: 求  1(m)到n直接有多少个数字x满足 x可以整出这个数字的每一位上的数字 思路: 整除每一位.只需要整除每一位的lcm即可 但是数字太大,dp状态怎么表示呢 发现 1~9的LCM 是2 ...

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

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

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

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

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

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

  9. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...

随机推荐

  1. 04 javascirpt基础知识---听课笔记

    1.JavaScript概念 一门客户端脚本语言运行在客户端浏览器中的.每一个浏览器都有JavaScript的解析引擎脚本语言:不需要编译,直接就可以被浏览器解析执行了 * 功能:可以来增强用户和ht ...

  2. MySQL数据库去重 SQL解决

    MySQL数据库去重的方法 ​ 数据库最近有很多重复的数据,数据量还有点大,本想着用代码解决,后来发现用SQL就能解决,这里记录一下 看这条SQL DELETE consum_record FROM ...

  3. day36——死锁、递归锁、信号量、GIL、多线程实现socket通信、线程池和进程池

    day36 死锁现象与递归锁 死锁现象 是指两个或两个以上的进程或线程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去.此时称系统处于死锁状态或系统产生了死锁,这 ...

  4. NLP自然语言处理的开发环境搭建

    NLP的开发环境搭建主要分为以下几步: Python安装 NLTK系统安装 Python3.5下载安装 下载链接:https://www.python.org/downloads/release/py ...

  5. STM32F030-UART1_DMA使用提示

    STM32F030-UART1_DMA使用提示 前言: 今天把STM32F030C8T6的串口DMA学习了一下,为了加快各位研发人员的开发进度,避免浪费大量的时间在硬件平台上,写出个人代码调试的经验. ...

  6. A Simple Question of Chemistry

    #include<stdio.h> int main() { int i, l; ]; ]; l = ; ) { l++; } ; a[i]!= && i<l; i+ ...

  7. Android--创建快捷方式

    需要权限: <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" /&g ...

  8. python模块之openpyxl

    这是一个第三方库,可以处理xlsx格式的Excel文件.pip install openpyxl安装.如果使用Aanconda,应该自带了. 读取Excel文件 需要导入相关函数. from open ...

  9. 全栈项目|小书架|服务器开发-Koa2 连接MySQL数据库(Navicat+XAMPP)

    为什么使用数据库 为什么需要数据库?-知乎 相比与文件系统,数据库具有以下优势: 高效率:查找效率高 高可用:可数据库共享 安全性强:数据不能随意修改 选择哪个数据库 数据库可以分为关系型数据库和非关 ...

  10. Disruptor底层源码解析(九)

    架构图: 性能为什么这么牛逼: public void sendData(ByteBuffer data) { //1 在生产者发送消息的时候, 首先 需要从我们的ringBuffer里面 获取一个可 ...