beautiful number 数位DP codeforces 55D
题目链接:
http://codeforces.com/problemset/problem/55/D
数位DP
题目描述:
一个数能被它每位上的数字整除(0除外),那么它就是beautiful number。问区间[a,b]上有多少个beautiful number。如102就是一个beautiful number,因为它能整除1,2。14不是,因为14不能整除4.
解法:
数位DP,设dp[i][j][k]为累计到第i为,公倍数为j,模lcm(1,2,```,9)=2520的余数为k的数的个数。注意到两个事实,要求某个数能整除它的每一个非0位上的数字,那么等价于将这些数字求一个最小公倍数,如果这个数能整除它的最小公倍数,自然就是beautiful number。已知lcm(1,2,···,9) = 2520,一个数一定能写成k+2520*x这样的形式,其中0<k<2520。设组成这个数的非0数字的最小公倍数为j,就有(k+2520*x)%j = k%j.同时能知道2520的因子(不一定是素数因子)一共有48个,所以离散的存储这些数,同时用ra[i],记录在离散数组中的编号。
贴代码:
#include <cstdio>
#include <cstring>
const int mod = ;
using namespace std;
typedef long long int LL;
template<typename T>T gcd(T a,T b)
{
return b==?a:gcd(b,a%b);
}
template<typename T>T lcm(T a,T b)
{
if(a*b == ) return a?a:b;
return a/gcd(a,b)*b;
}
int ra[mod+];
int lm[];
LL dp[][][mod+];
int e[];
int p[];
void init()
{
e[] =;
for(int i=; i<; ++i)
e[i] = e[i-]*%mod; //e[i]表示10^i%2520的余数
int cnt=;
for(int i=; i<=mod; ++i)
if(mod%i == ) lm[cnt] = i,ra[i] = cnt,++cnt;//记录2520的因子
//ra记录这个因子在离散化存因子中的编号
}
void onceInit()
{
init();
dp[][][] = ;
for(int i=; i<; ++i)
{
for(int t=; t<; ++t)
{
for(int j=; j<; ++j)
{
int d = lcm(lm[j],t);
int jj = ra[d];
for(int k=; k<; ++k)
{
int kk = (t*e[i-]+k)%mod;
dp[i][jj][kk] += dp[i-][j][k];
}
}
}
}
}
int splitInt(LL x)//将数拆成一位一位的存在p数组中
{
int i;
for(i=; x; ++i)
p[i] = x%,x /= ;
return i;
}
LL solve(LL x)//统计从0-x中有多少个beautiful number,x不包含在内
{
LL ans =;
int len = splitInt(x);
int cu1=,cu2=;//前面数的公倍数,余数
for(int i=len-; i> ; --i)
{
for(int t=; t<p[i]; ++t)
{
for(int j=; j<; ++j)
{
int d = lcm(lm[j],t);
d = lcm(d,cu1);//这是真正的公倍数
int tmp = (cu2+t)*e[i-]%d;//k+tmp = l*d这样的k会是解
for(int k=(d-tmp)%d; k < ; k +=d)
ans += dp[i-][j][k];
}
}
cu1=lcm(cu1,p[i]);//更新前面的余数和倍数
cu2 = (cu2+p[i])*%mod;
}
return ans;
}
int main()
{
// freopen("in.c","r",stdin);
onceInit();
int t;
scanf("%d",&t);
while(t--)
{
LL a,b;
scanf("%I64d%I64d",&a,&b);
printf("%I64d\n",solve(b+) - solve(a));
}
return ;
}
beautiful number 数位DP codeforces 55D的更多相关文章
- HDU 5179 beautiful number 数位dp
题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5179 bc(中文): http://bestcoder.hdu.edu.cn/contes ...
- FZU2179/Codeforces 55D beautiful number 数位DP
题目大意: 求 1(m)到n直接有多少个数字x满足 x可以整出这个数字的每一位上的数字 思路: 整除每一位.只需要整除每一位的lcm即可 但是数字太大,dp状态怎么表示呢 发现 1~9的LCM 是2 ...
- beautiful number 数位dp
题意: 求高位往低位递减且 高位%低位==0(相邻) 数字数量 唯一要注意的就是前导零!!!!!!(正因为这个前导零 一开始的pre设置为0 ) 比如 11 10 09 08 ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...
- 多校5 HDU5787 K-wolf Number 数位DP
// 多校5 HDU5787 K-wolf Number 数位DP // dp[pos][a][b][c][d][f] 当前在pos,前四个数分别是a b c d // f 用作标记,当现在枚举的数小 ...
- 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+数论)
题意:求[L,R](1<=L<=R<=9e18)区间中所有能被自己数位上的非零数整除的数的个数 分析:丛数据量可以分析出是用数位dp求解,区间个数可以转化为sum(R)-sum(L- ...
- Codeforces Beta Round #51 D. Beautiful numbers 数位dp
D. Beautiful numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/p ...
随机推荐
- windows7 桌面突然卡住了,点击右键点不了,点击桌面软件点不了,怎么办?
关闭并重启explorer.exe进程命令操作 :1. cmd 2. taskkill /f /im explorer.exe && start explorer.exe
- web前端设计规范
hi,这里写出一点自己对web产品开发的一点粗浅的规范认识,一切为了敏捷开发哈哈. 1.流程. (1) 当产品给出原型和产品文档. (2)设计师更据原型,开始设计产品的效果图. (3)设计师设计完毕后 ...
- m_Orchestrate learning system---三十三、公共变量多弄成全局变量
m_Orchestrate learning system---三十三.公共变量多弄成全局变量 一.总结 一句话总结:比如班级id,小组id,这样省事,而且减少数据库的访问,加快访问速度,而且节约代码 ...
- 雷林鹏分享:Ruby 判断
Ruby 判断 Ruby 提供了其他现代语言中很常见的条件结构.在这里,我们将解释所有的条件语句和 Ruby 中可用的修饰符. Ruby if...else 语句 语法 if conditional ...
- English trip -- Review Unit1 Personal Information 个人信息
1.重点内容进行自我介绍 What's you name? I'm Loki Where are you from? I'm Local, I'm Chengdu How old are you? t ...
- English trip -- VC(情景课)2 D Reading
Xu言: 业精于勤,荒于嬉:行成于思,毁于随 Before you read 阅读准备 Talk about the picture, what do you see?看图说话,你看到了什么? Lis ...
- Java基础-String和StringBuilder类型(11)
String类概述 字符串是由多个字符组成的一串数据字符串可以看成是字符数组 构造方法 public String(String original)public String(char[] value ...
- MVC ——设置启动 URL
Visual Studio 会以一种有助的尝试,根据当前正在编辑的视图,让浏览器请求一个 URL.但这是一个不稳定的特性. 为了对浏览器的请求设置一个固定的 URL,可以从 Visual Studio ...
- java标号
标号用于控制循环执行流程: public static void main(String[] args) { mark: for(int i = 0; i < 3; i++) { System. ...
- 纯js倒计时效果(交流加群:452892873)(本群每天都更新学习资料)
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...