CodeForces - 55D Beautiful numbers(数位DP+Hash)题解
题意:美丽数定义:一个正数能被所有位数整除。求给出一个范围,回答这个范围内的美丽数。
思路:一个数能被所有位数整除,换句话说就是一个数能整除所有位数的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)题解的更多相关文章
- codeforces 55D - Beautiful numbers(数位DP+离散化)
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- CodeForces - 55D - Beautiful numbers(数位DP,离散化)
链接: https://vjudge.net/problem/CodeForces-55D 题意: Volodya is an odd boy and his taste is strange as ...
- CodeForces - 55D Beautiful numbers —— 数位DP
题目链接:https://vjudge.net/problem/CodeForces-55D D. Beautiful numbers time limit per test 4 seconds me ...
- Codeforces - 55D Beautiful numbers (数位dp+数论)
题意:求[L,R](1<=L<=R<=9e18)区间中所有能被自己数位上的非零数整除的数的个数 分析:丛数据量可以分析出是用数位dp求解,区间个数可以转化为sum(R)-sum(L- ...
- codeforces 55D. Beautiful numbers 数位dp
题目链接 一个数, 他的所有位上的数都可以被这个数整除, 求出范围内满足条件的数的个数. dp[i][j][k], i表示第i位, j表示前几位的lcm是几, k表示这个数mod2520, 2520是 ...
- FZU2179/Codeforces 55D beautiful number 数位DP
题目大意: 求 1(m)到n直接有多少个数字x满足 x可以整出这个数字的每一位上的数字 思路: 整除每一位.只需要整除每一位的lcm即可 但是数字太大,dp状态怎么表示呢 发现 1~9的LCM 是2 ...
- CF 55D. Beautiful numbers(数位DP)
题目链接 这题,没想出来,根本没想到用最小公倍数来更新,一直想状态压缩,不过余数什么的根本存不下,看的von学长的blog,比着写了写,就是模版改改,不过状态转移构造不出,怎么着,都做不出来. #in ...
- 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的水题,觉得 ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...
随机推荐
- Android 长截屏原理
https://android-notes.github.io/2016/12/03/android%E9%95%BF%E6%88%AA%E5%B1%8F%E5%8E%9F%E7%90%86/ a ...
- c# Sockect 通信
1.Server using System; using System.Collections.Generic; using System.Text; //添加Socket类 using System ...
- ionic开发过程中遇到的一些坑!
总结一些:在使用 ionic 开发过程中所遇到的问题. 问题一:Cannot find module '@ionic/app-scripts' 描述:使用 ionic start 项目的时候,项目安装 ...
- jquery实现ajax跨域请求!亲测有效
在解决跨域的时候,我通常会用豆瓣api作为尝试. 下面是本地跨域请求豆瓣API:亲测有效: <script type="text/javascript"> var ur ...
- 04Add.ashx(新增班级)
04Add.html <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <hea ...
- CodeForces 17D Notepad(同余定理)
D. Notepad time limit per test 2 seconds memory limit per test 64 megabytes input standard input out ...
- flask的session用法2
from flask import Flask, session, redirect, url_for, escape, request app = Flask(__name__) @app.rout ...
- python https协议和InsecurePlatformWarning问题
本人最近在学习python,今天想使用python来抓取糗事百科网站上的一些笑话故事的,由于糗事百科的网站url采取的是https协议,所以当我按照常规的方式抓取的时候,发现不行,报错了,找了很多方法 ...
- Django - 常用配置
一.logging配置 Django项目常用的logging配置 settings.py LOGGING = { 'version': 1, 'disable_existing_loggers': F ...
- Python开发【杂货铺】:写code经常记不住的事儿
1.添加系统环境变量: 每次写程序,把程序路径添加到环境变量中时,总是磕磕绊绊忘一些,搞得总是从之前的程序里直接copy # 程序目录添加到系统环境变量 import os import sys im ...