题目链接:

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的更多相关文章

  1. HDU 5179 beautiful number 数位dp

    题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5179 bc(中文): http://bestcoder.hdu.edu.cn/contes ...

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

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

  3. beautiful number 数位dp

    题意:  求高位往低位递减且  高位%低位==0(相邻) 数字数量 唯一要注意的就是前导零!!!!!!(正因为这个前导零   一开始的pre设置为0       ) 比如  11  10 09 08 ...

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

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

  5. 多校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 用作标记,当现在枚举的数小 ...

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

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

  7. CodeForces - 55D - Beautiful numbers(数位DP,离散化)

    链接: https://vjudge.net/problem/CodeForces-55D 题意: Volodya is an odd boy and his taste is strange as ...

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

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

  9. 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 ...

随机推荐

  1. es6模块 nodejs模块和 typescript模块

    es6模块 import和export nodejs模块 require和module.exports typescript模块 module和export

  2. Oracle数据库system用户忘记了密码怎么办

    1.在运行里面输入cmd调出dos窗口,然后在dos窗口中输入sqlplus /nolog 如:D:\oracle\ora92\bin>sqlplus /nolog 2.输入连接命令 如:SQL ...

  3. Failed to execute operation: No such file or directory(systemctl enable iptables.service)

    在保存Iptables配置时:systemctl enable iptables.service 出现错误: Failed to execute operation: No such file or ...

  4. bzoj1833: [ZJOI2010]count 数字计数 数位dp

    bzoj1833 Description 给定两个正整数a和b,求在[a,b]中的所有整数中,每个数码(digit)各出现了多少次. Input 输入文件中仅包含一行两个整数a.b,含义如上所述. O ...

  5. python-day21--time模块

    一.三种表示方法 1.时间戳(timestamp): time.time( )       #得到的是float类型 2.格式化(Format String): time.strftime('%Y/% ...

  6. 用了皮肤控件之后,报错:容量超出了最大容量 参数名:capacity

    用了皮肤控件之后,报错:容量超出了最大容量 参数名:capacity MessageBox.show()错误!!容量超出了最大容量.参数名: capacity 解决方案: 设置 skin.SkinDi ...

  7. ajax乱码解决汇总

    ajax乱码解决总结第一,javascript沿用java的字符处理方式,内部是使用unicode来处理所有字符的,第二,utf-8是每个汉字(unicode字符)用3个字节来存储.第三,用utf-8 ...

  8. FNDLOAD移植Lookup Type

    通过OAF WEB页面添加的lookup type不能使用fndload直接移植,移植之后无法包含code值,必须使用FORM窗口定义.

  9. 常见MIME类型例表

    常见MIME类型例表: 序号 内容类型 文件扩展名 描述 1 application/msword doc Microsoft Word 2 application/octet-stream bin ...

  10. 管道的创建与读写pipe

    1.管道的创建 #include <unistd.h> int pipe(int pipefd[2]); linux下创建管道可以通过函数pipe来完成.该函数如果调用成功,数组中将包含两 ...