题目链接:

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. U3D协程yield的使用和理解

    部分内容参考网址:http://blog.csdn.net/huang9012/article/details/29595747 Win7+U3D 4.6.7 1.在c#中使用①首选需要定义一个返回值 ...

  2. English trip -- VC(情景课)3 A Family

    xu言: 今天,老天很给面子.我在路上的时候基本上没有~然而我也没有带雨具.难道这就是传中的天道酬勤~或者说只要你努力,老天爷会给让路 Talk about the picture 看图说话 Look ...

  3. LeetCode--038--报数(*)

    问题描述: 报数序列是指一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1. 1 2. 11 3. 21 4. 1211 5. 111221 1 被读作  "one ...

  4. 利用nodeJs anywhere搭建本地服务器环境

    1.npm install anywhere -g 如果是mac系统会提示你权限不够,需要在代码前加上 sudo获取管理员权限.即sudo npm install anywhere -g. 2.安装完 ...

  5. VS2015新建项目时,左侧面板空白的解决方法

    解决办法是: 1.进入"C:\Users\当前用户名(一般为administrator)\AppData\Local\Microsoft\VisualStudio\14.0" 2. ...

  6. ThreadPoolExecutor类

    首先分析内部类:ThreadPoolExecutor$Worker //Worker对线程和任务做了一个封装,同时它又实现了Runnable接口, //所以Worker类的线程跑的是自身的run方法 ...

  7. Cxfreeze使用存在问题

    Cxfreeze使用 cx_Freeze-5.1.1-cp36-cp36m-win_amd64.wh 1● 打包多个文件 Cxfreeze D:/test.py –target-dir D:/   c ...

  8. 标准库中 vector list等排序

    1.list自带有排序函数sort():可以定义自己的排序规则,如: struct stTest { int count; wstring str; }; bool SortByNum(const s ...

  9. Bellman-Ford模板

    转载链接:http://blog.csdn.net/niushuai666/article/details/6791765 Dijkstra算法是处理单源最短路径的有效算法,但它局限于边的权值非负的情 ...

  10. 关于Ubuntu 常用的简单指令

    这几天工作强度不算太高,就自己学了一下linux,我就把一些简单的指令整理了一下,希望以后有参考: 我是用的VMware 安装的Ubuntu 虚拟机: 下面直接贴出我整理的简单的日常使用的指令 创建文 ...