codeforces 55D. Beautiful numbers (数位dp)
来源:互联网 发布:淘宝客app定制 编辑:程序博客网 时间:2024/05/07 15:05
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
思路:一个数能够整除所有位上的数,那么它就能够整数所有位上的值的最小公倍数。因为一个位数上的数为1~9,它们的最小公倍数最大为2520,而且所有情况的最小公倍数的数量为48个。又因为所有最小公倍数都能整除2520,即都是2520的约数,所以我们可以先存下前面所代表的十进制数模上2520的余数yushu,并且记录前面的所有位上的数的最小公倍数lcm,那么最后如果yushu%lcm==0,就表示这个数可行。这里还要注意,最小公倍数不用从1~2520依次枚举,因为可行的就只有48个,需要hash一下。
#include<iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<string>#include<bitset>#include<algorithm>using namespace std;typedef long long ll;typedef long double ldb;#define inf 99999999#define pi acos(-1.0)#define MOD 1000000007#define maxn 100050int has[55]={0,1,2,3,4,5,6,7,8,9,10,12,14,15,18,20,21,24,28,30,35,36,40,42,45,56,60,63,70,72,84,90,105,120,126,140,168,180,210,252,280,315,360,420,504,630,840,1260,2520};ll dp[25][2550][55];int wei[30];int gcd(int a,int b){ return b ? gcd(b,a%b) : a;}ll dfs(int pos,int yushu,int index,int flag){ int i,j; if(pos==0){ if(yushu%has[index]==0)return 1; else return 0; } if(!flag && dp[pos][yushu][index ]!=-1)return dp[pos][yushu][index ]; int ed=flag?wei[pos]:9; int yushu1,lcm1,index1; ll ans=0; for(i=0;i<=ed;i++){ yushu1=(yushu*10+i)%2520; if(i==0)index1=index; else{ int lcm=has[index]; lcm1=lcm*i/gcd(lcm,i); index1=lower_bound(has,has+49,lcm1)-has; } ans+=dfs(pos-1,yushu1,index1,flag&&(i==ed) ); } if(!flag)dp[pos][yushu][index]=ans; return ans;}ll solve(ll x){ int i,j,len=0; ll t=x; //memset(dp,-1,sizeof(dp)); while(t){ wei[++len]=t%10; t/=10; } return dfs(len,0,1,1);}int main(){ int i,j,T; ll n,m; memset(dp,-1,sizeof(dp)); scanf("%d",&T); while(T--) { scanf("%I64d%I64d",&m,&n); printf("%I64d\n",solve(n)-solve(m-1)); } return 0;}
- 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)
- codeforces 55D Beautiful numbers 数位dp
- Codeforces 55D Beautiful numbers 数位DP
- CodeForces 55D Beautiful numbers 数位dp
- codeforces 55D D. Beautiful numbers(数位dp+数论)
- android:xx 配置解释
- java学习笔记(一)System.out.println探究
- 用Eclipse编写Java时调用algs4.jar和stdlib.jar解决StdOut和StdIn无法被识别的问题
- Spring MVC 数据绑定多个对象
- Thinkphp调用ajaxReturn中文乱码解决办法
- codeforces 55D. Beautiful numbers (数位dp)
- Spark 1.6.0 单机安装配置
- codeforces 631E Product Sum (dp凸单调性)
- 在公司编程的大方向上的感悟
- hdu 5635 LCP Array【思维】
- Log4J配置
- 何以诚快变成神经哥了
- 郭霖博客处转载的超赞的Volley用法
- android studio上Svn环境之新手搭建,错误解决。