题意:美丽数定义:一个正数能被所有位数整除。求给出一个范围,回答这个范围内的美丽数。

思路:一个数能被所有位数整除,换句话说就是一个数能整除所有位数的LCM,所以问题就转化为一个数能否被所有位数的LCM整除。按照一般的思想,直接开三维dp[pos][num][lcm]。但是num范围很大,直接开就爆了,怎么办呢?我们可以把num%2520(即1~9的LCM)储存为mod,因为所有位数最大的LCM就是2520,所以可以mod 2520。然后你很开心的开了dp[pos][2520][2520],然后你又爆了...

然后你可以发现虽然LCM最大是2520,但是没有一个LCM是2519这样的数字,这样就有很多空间浪费,打个表可以发现,1~9的LCM其实就48个数字,我们把这48个数字Hash一下保存,那么就缩小到了dp[pos][2520][48]。

然后你就可以A了

友谊赛被打爆了,水题都不会了

参考

代码:

#include<cstdio>
#include<map>
#include<set>
#include<vector>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define ll long long
const int N = 50000+5;
const int INF = 0x3f3f3f3f;
using namespace std;
int dig[20];
ll dp[20][2550][50],Hash[2550];
ll Gcd(ll a,ll b){
return b == 0? a : Gcd(b,a%b);
}
ll Lcm(ll a,ll b){
return a * b / Gcd(a,b);
}
ll dfs(int pos,int mod,int lcm,bool limit){
if(pos == -1) return mod % lcm == 0? 1 : 0;
if(!limit && dp[pos][mod][Hash[lcm]] != -1) return dp[pos][mod][Hash[lcm]];
int top = limit? dig[pos] : 9;
ll ret = 0;
for(int i = 0;i <= top;i++){
int MOD = (mod*10 + i) % 2520;
int LCM;
if(i) LCM = Lcm(i,lcm);
else LCM = lcm;
ret += dfs(pos - 1,MOD,LCM,limit && i == top);
}
if(!limit) dp[pos][mod][Hash[lcm]] = ret;
return ret;
}
ll solve(ll x){
int pos = 0;
while(x){
dig[pos++] = x % 10;
x /= 10;
}
ll ret = dfs(pos- 1,0,1,true);
return ret;
}
int main(){
int T;
ll l,r,cnt = 0;
scanf("%d",&T);
memset(dp,-1,sizeof(dp));
for(int i = 1;i*i <= 2520;i++){ //hash
if(2520 % i == 0){
Hash[i] = cnt++;
if(i*i != 2520){
Hash[2520 / i] = cnt++;
}
}
}
while(T--){
scanf("%I64d%I64d",&l,&r);
printf("%I64d\n",solve(r) - solve(l - 1));
}
return 0;
}

CodeForces - 55D Beautiful numbers(数位DP+Hash)题解的更多相关文章

  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 题意: Volodya is an odd boy and his taste is strange as ...

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

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

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

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

  5. codeforces 55D. Beautiful numbers 数位dp

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. console.log()的兼容性

    在别人那里看到的,兼容IE8-的console.log的实现,以前没想过. if(typeof console == "undefinde"){ this.console = {l ...

  2. Eclipse '<>' operator is not allowed for source level below 1.7

    '<>' operator is not allowed for source level below 1.7 解决方法:

  3. vscode中的vue文件中emmet进行tab键不起作用

    文件--首选项---设置 搜索: emmet.includeLanguages在右边修改 "emmet.triggerExpansionOnTab": true, "em ...

  4. SQL中 decode()函数简介(转载)

    今天看别人的SQL时看这里面还有decode()函数,以前从来没接触到,上网查了一下,还挺好用的一个函数,写下来希望对朋友们有帮助哈! decode()函数简介: 主要作用:将查询结果翻译成其他值(即 ...

  5. ibatis 中#和 $ 符号的区别

    1.数据类型匹配 #:会进行预编译,而且进行类型匹配(自动确定数据类型): $:不进行数据类型匹配. 2.实现方式: # 用于变量替换(先生成一个占位符,然后替换) select * from use ...

  6. WebConfig配置详解大全

    <?xml version="1.0"?> <!--注意: 除了手动编辑此文件以外,您还可以使用 Web 管理工具来配置应用程序的设置.可以使用 Visual S ...

  7. Oracle归档文件夹权限设置错误导致的数据库问题解决

    把oracle设置为归档模式并且为归档文件新建文件夹 /home/oracle/app/oracle/arch/orcl 但是在启动或者备份时候经常性出现错误 startup报错 startup同时日 ...

  8. 2018CCPC秦皇岛站

    这次去秦皇岛,两个队都打铁回来,真的是蛮耻辱的 可以说是有史以来最差成绩了 其实网络赛就打的不好 两个名额一个是省赛分配的一个是排队排来的 去之前拉了几场之前的CCPC的比赛打 感觉打的都还不错的 热 ...

  9. #pragma预处理命令详解

    #pragma预处理命令 #pragma可以说是C++中最复杂的预处理指令了,下面是最常用的几个#pragma指令: #pragma comment(lib,"XXX.lib") ...

  10. xplan-打印执行顺序

    -- ------------------------------------------------------------------------------------------------- ...