Codeforces 55D Beautiful Number (数位dp)
来源:互联网 发布:台达plc编程教学视频 编辑:程序博客网 时间:2024/05/21 05:55
题意:
统计某段区间内满足它所有非零数位能整除这个数本身的数的个数。
思路:
首先这题要知道:一个数字要被它的所有非零位整除,即被他们的LCM整除。
然后,常见数位dp定义方式dp[len][presum]:表示长度为len并且前缀和为presum的满足条件数的个数。
所以这题根据题目要求的条件还需要一维状态记载前缀所有数的最小公倍数。{1~9}所有组合不同最小公倍数有48个,最大为2520.所以离散化处理48个不同最小公倍数。
状态定义就定义:dp[len][presum][prelcm]。
又由于presum范围9*10^18。而根据mod性质:
persum % LCM{digit[xi]} = 0
即 presum % MOD % LCM{digit[xi]} = 0
所以可以只需存presum % MOD,范围缩小了
而MOD就是{1~9}最大公倍数2520.
总的时间复杂度:状态数*决策数:19*2520*48*10
#include<bits/stdc++.h>using namespace std;typedef long long ll;const int mod=2520;ll dp[20][2580][50];ll gcd(ll a,ll b){return b==0? a : gcd(b,a%b);}ll lcm(ll a,ll b){return a/gcd(a,b)*b;}int index[2580],dig[20];void init(){ int p=0; for(int i=1;i<=mod;i++){ if(mod%i==0) index[i]=p++; } memset(dp,-1,sizeof(dp));}ll dfs(int pos,int presum,int prelcm,bool limit){ if(pos<1) return presum%prelcm==0; if(!limit&&dp[pos][presum][index[prelcm]]!=-1) return dp[pos][presum][index[prelcm]]; int ed=limit ? dig[pos]:9; ll ans=0; for(int i=0;i<=ed;i++){ int nowlcm=prelcm; int nowsum=(presum*10+i)%mod; if(i) nowlcm=lcm(prelcm,i); ans+=dfs(pos-1,nowsum,nowlcm,limit&&i==ed); } if(!limit) dp[pos][presum][index[prelcm]]=ans; return ans;}ll solve(ll x){ int pos=0; memset(dig,0,sizeof(dig)); while(x){ dig[++pos]=x%10; x/=10; } return dfs(pos,0,1,true);}int main(){ init(); int T;scanf("%d",&T); while(T--){ ll a,b;scanf("%I64d %I64d",&a,&b); printf("%I64d\n",solve(b)-solve(a-1)); }}
0 0
- 【CodeForces 55D】Beautiful number 数位DP
- Codeforces 55D Beautiful Number (数位dp)
- CF-55D-数位DP-Beautiful Number
- Codeforces 55D Beautiful Number (数位统计)
- Codeforces 55D Beautiful numbers 数位dp
- Codeforces 55D Beautiful numbers --- 数位DP
- Codeforces 55D Beautiful numbers 数位dp
- 【codeforces】55D. Beautiful numbers 数位DP
- [CodeForces 55D] Beautiful numbers && 数位DP
- Codeforces 55D Beautiful numbers 数位DP
- 【数位DP】 Codeforces 55D Beautiful numbers
- codeforces 55D Beautiful numbers[数位dp]
- [Codeforces 55D]Beautiful numbers(数位DP)
- CodeForces 55D Beautiful numbers (数位DP)
- Codeforces 55D Beautiful numbers【数位dp】
- codeforces 55D. Beautiful numbers (数位dp)
- CodeForces 55D Beautiful numbers(数位dp)
- codeforces-55D-Beautiful numbers(数位DP)
- Android(Java)编译时注解修改代码
- 进制转换:二,八,十,十六进制互相转换
- Spark学习——RDD基本操作
- Spring boot 定制banner
- 6.流程控制
- Codeforces 55D Beautiful Number (数位dp)
- 微信小程序文件上传java服务器端wx:uploadFile()
- SEO笔记
- ThreadLocalRandom
- Xcode8 及iOS10适配问题汇总
- Hadoop问题解决之:java.lang.ClassNotFoundException:com.demo.Demo$DemoMapper
- 警告: Could not find action or result There is no Action mapped for namespace / and action name
- Hadoop(一) 之 安装Hadoop
- iOS 常用三方库