codeforce 55 D. Beautiful numbers(数位dp,好题)
来源:互联网 发布:excel表格怎么填充数据 编辑:程序博客网 时间:2024/05/22 06:48
Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue with this and just count the quantity of beautiful numbers in given ranges.
The first line of the input contains the number of cases t (1 ≤ t ≤ 10). Each of the next t lines contains two natural numbers li and ri(1 ≤ li ≤ ri ≤ 9 ·1018).
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cin (also you may use %I64d).
Output should contain t numbers — answers to the queries, one number per line — quantities of beautiful numbers in given intervals (from li to ri, inclusively).
11 9
9
112 15
2
题意:
一个美丽数就是可以被它的每一位的数字整除的数。给定一个区间,求美丽数的个数。
题解:
传送门
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<vector>using namespace std;typedef long long ll;const int MOD=2520;ll d[20][MOD][50];int H[MOD],bit[20];int cnt=0;int gcd(int a,int b){return b==0? a:gcd(b,a%b);}int lcm(int a,int b){return a/gcd(a,b)*b;}void init(){cnt=0;for(int i=1;i<=MOD;i++) if(MOD%i==0) H[i]=++cnt;}ll dp(int pos,int mod,int l,int f){if(pos==-1) return (mod%l==0);if(!f&&d[pos][mod][H[l]]!=-1) return d[pos][mod][H[l]];ll ans=0;int end=f? bit[pos]:9;ans+=dp(pos-1,mod*10%MOD,l,f&&(bit[pos]==0));for(int i=1;i<=end;i++)ans+=dp(pos-1,(mod*10+i)%MOD,lcm(l,i),f&&(i==end));if(!f) d[pos][mod][H[l]]=ans;return ans;}ll cal(ll x){int num=0;while(x){bit[num++]=x%10;x/=10;}return dp(num-1,0,1,1);}int main(){int cas;scanf("%d",&cas);memset(d,-1,sizeof(d));init();while(cas--){ll a,b;scanf("%I64d%I64d",&a,&b);printf("%I64d\n",cal(b)-cal(a-1));}return 0;}
- codeforce 55 D. Beautiful numbers(数位dp,好题)
- Codeforces 55D - Beautiful numbers(数位dp)好
- Codeforces 55D Beautiful numbers 数位dp
- 数位dp CF 55 D. Beautiful numbers
- Codeforces 55D Beautiful numbers --- 数位DP
- 数位dp CF 55D Beautiful numbers
- Codeforces 55D Beautiful numbers 数位dp
- [数位dp] cf 55D Beautiful numbers
- 【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)
- CF 55D Beautiful numbers (数位DP)
- Codeforces 55D Beautiful numbers【数位dp】
- codeforces 55D. Beautiful numbers (数位dp)
- VS2013中 勾选在共享DLL中使用MFC 报错 系统找不到指定文件 参考解决办法
- 排序和插入
- 手机备忘录
- Docker学习
- U盘安装CentOS 6.4 + Windows 7双系统 (Windows 7下安装 CentOS 6.4)
- codeforce 55 D. Beautiful numbers(数位dp,好题)
- Codeforces 727A dfs打印路径
- 50本永不过时的经典计算机书籍
- 《面筋:拿到美团点评和去哪儿网的offer》
- 一加手机刷第三方Recovery、获取Root权限、刷CM教程
- List、Set集合Map集合的遍历
- TCP交互数据流,成块数据流(非原创)
- 腾讯云centos7.2安装mysql
- MAC环境下安装和配置Nginx