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 ...
随机推荐
- 链表排序 Sort List
2018-08-11 23:50:30 问题描述: 问题求解: 解法一.归并排序 public ListNode sortList(ListNode head) { if (head == null ...
- C# DataTable列名不区分大小写
一直很纠结的就是DataTable的列名如何才能规范,从Oracle取出的DataTable都是大写,最后尝试了一下,原来C#的DataTable列名并不区分大小写,具体例子如下: DataTable ...
- 错误代码0x00000001,好多软件连不了网,求助~(WIN7/win8/win9/win10)
解决办法: 以管理员身份运行命令行,在弹出的窗口中运行如下命令: netsh winsock reset catalog netsh int ip reset reset.log hit 让被阻止了的 ...
- 雷林鹏分享:C# 预处理器指令
C# 预处理器指令 预处理器指令指导编译器在实际编译开始之前对信息进行预处理. 所有的预处理器指令都是以 # 开始.且在一行上,只有空白字符可以出现在预处理器指令之前.预处理器指令不是语句,所以它们不 ...
- Analysis of single cell RNA-seq data(单细胞终极课程)
业界良心啊,开源的单细胞课程. 随便看了几章,课程写得非常用心,非常适合新手. 课程地址:Analysis of single cell RNA-seq data 源码地址:hemberg-lab/s ...
- English trip -- VC(情景课) 7 C How much are the shose? 鞋多少钱
Grammar focus 语法点: How much is ...? How much are...? How much is the shirt? $15.99. How much are ...
- android--------自定义视频控件(视频全屏竖屏自动切换)
android播放视频也是常用的技术,今天分享一个自定义视频控件,支持自定义控制 UI,全屏播放, 可以实现自动横竖屏切换的控件,跟随手机的位置而,重力感应自动切换横竖屏. 效果图: 代码下载Gi ...
- Sergey's problem CodeForces - 1019C (图论,构造,神题)
链接 大意: 给定有向图, 求选择一个点集$S$, 使得$S$任意两点不相连, 且对于不属于$S$的任意点$x$, 均存在$S$中的点$y$, 使得$d(x,y)<=2$, $d(x,y)$为从 ...
- node -- hapi 学习
node learning 学习node,是为了后续项目可以正常开展,现在写个项目,若不是连接后台,请求数据,一切都不叫着项目了.正好借助掘金的小册,来推进学习 学习资料 YouTube 1 掘金 h ...
- BZOJ一天提交(AC) 51纪念
像我这种蒟蒻...一天交了51道题也要纪念... 真是...但是.不知道还有没有机会了... 这样子就会有很多题解要补了...慢慢来吧... 另.AC数已经超过500了还真是历史性的一步啊...