【cf】55d beautiful numbers【精妙的数位dp+离散化】
来源:互联网 发布:和平网络电视怎么用 编辑:程序博客网 时间:2024/05/16 07:11
题意:
求1-n中,能被所有组成他的非0数整除的数的个数
题解:
题意很简单,但是考虑到数位dp的状态转移,dfs(pos,pre,status,limit)如果要记录除以他所有数那么势必要用数组来存,但这样一来,很难用dp数组来记录当前状态,这题非常巧妙的采用了lcm的方法,避免了该类操作,2-9的lcm是2520,这样我们只要记录前面数字的lcm和前面数字模2520剩下的数,那么既保证了不会漏掉前面的状态,又保证了当前状态的可记录性,但是这样dp数组要开到[20][2520][2520]还是会接近爆内存,我们考虑到可以把lcm离散化,因为2-9的lcm在2520区间中总共只有48个,所以dp数组可以压缩成dp[20][50][2530],这样这个问题就完美的解决了。
#include<cstdio>#include<cstring>using namespace std;#define ll __int64ll dp[30][50][2530];int Hash[2530],bit[30];ll b,a;int T;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;}ll dfs(int pos,int prelcm,int prenum,int limit){ if(pos==0) return prenum%prelcm==0; if(!limit&&dp[pos][Hash[prelcm]][prenum]!=-1)return dp[pos][Hash[prelcm]][prenum]; int num=limit?bit[pos]:9; ll ret=0; for(int i=0;i<=num;i++){ int prelcm1=prelcm; if(i)prelcm1=lcm(prelcm,i); int prenum1=(prenum*10+i)%2520; ret+=dfs(pos-1,prelcm1,prenum1,limit&&(i==num)); } if(!limit)dp[pos][Hash[prelcm]][prenum]=ret; return ret;}ll solve(ll n){ int len=0; while(n){ bit[++len]=n%10; n/=10; } return dfs(len,1,0,1);}int main(){//freopen("C:\\Users\\Administrator\\Desktop\\input.txt","r",stdin); scanf("%d",&T); int cnt=0; for(int i=1;i<=2520;i++)if(2520%i==0)Hash[i]=++cnt; memset(dp,-1,sizeof(dp)); while(T--){ scanf("%I64d%I64d",&a,&b); printf("%I64d\n",solve(b)-solve(a-1)); } return 0;}
0 0
- 【cf】55d beautiful numbers【精妙的数位dp+离散化】
- 数位dp CF 55 D. Beautiful numbers
- 数位dp CF 55D Beautiful numbers
- [数位dp] cf 55D Beautiful numbers
- CF 55D Beautiful numbers (数位DP)
- CodeForces 55D Beautiful numbers(数位dp&&离散化)
- CF D. Beautiful numbers (数位dp)
- #51 D. Beautiful numbers (数位dp+离散化)
- CF 55D - Beautiful numbers(数位DP)
- CF 55D Beautiful numbers(数位DP)
- CF 55D Beautiful numbers (数位DP入门)
- codeforces 55D Beautiful numbers(数位dp+离散化)【模板】
- Beautiful numbers [数位dp][离散化]
- 【数位DP+离散化】Beautiful numbers CodeForces
- CF-55D-数位DP-Beautiful Number
- Codeforces 55D Beautiful numbers 数位dp
- Codeforces 55D Beautiful numbers --- 数位DP
- Codeforces 55D Beautiful numbers 数位dp
- android图片重绘添加简单水印(日期等)
- class2_week6b
- Ubuntu14.04快速搭建SVN服务器及日常使用
- 在必须创建Activity的情况下又如何让Activity不显示
- java实现附件预览(openoffice+swftools+flexpaper)
- 【cf】55d beautiful numbers【精妙的数位dp+离散化】
- Cadence基础知识4(焊盘制作比较重要的3点以及热风焊盘的作用和制作 )
- 30 分钟 git 命令入门到放弃
- LIBSVM学习(二)在linux环境下安装LIBSVM
- Android BGABadgeView:BGABadgeLinearLayout以整体线性布局作为BadgeView(3)
- 【Netty4.x】Unity客户端与Netty服务器的网络通信(一)
- 开发时常用的数学函数
- java面向对象
- 最短路+记录路径+变形!!