CodeForces 55D - Beautiful numbers
来源:互联网 发布:网络优化工程师怎么学 编辑:程序博客网 时间:2024/05/12 10:22
Description
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.
Input
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
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).
Sample Input
1
1 9
9
1
12 15
2
#include <cstdio>#include <iostream>#include <cstring>#include <string>using namespace std;typedef long long LL; int a[20];const int MOD = 2520;LL dp[20][MOD][50];int hash[MOD];int GCD(int a, int b){ if(b==0) return a; return GCD(b, a%b); }int LCM(int a, int b){ return a/GCD(a,b)*b;}LL dfs(int len, int mod, int lcm, bool limit){ if(len < 1) return mod%lcm==0; if(!limit && dp[len][mod][hash[lcm]] != -1) return dp[len][mod][hash[lcm]]; int maxn = limit ? a[len] : 9; LL ret = 0; int new_lcm; for(int i=0; i<=maxn; i++) { if(i) new_lcm = LCM(lcm, i); else new_lcm = lcm; ret += dfs(len-1, (mod*10+i)%MOD, new_lcm, limit&&i==maxn); } if(!limit) dp[len][mod][hash[lcm]] = ret; return ret;}LL f(LL n){ int len = 0; while(n) { a[++len] = n % 10; n/=10; } return dfs(len, 0, 1, 1);}void Init(){ int cnt = 0; for(int i=1; i<=MOD; i++) { if(MOD % i == 0) { hash[i] = cnt++; } }// printf("cnt = %d\n", cnt); //}int main (){ Init(); int t; LL l, r; scanf("%d", &t); memset(dp, -1, sizeof(dp)); while(t--) { scanf("%I64d%I64d", &l, &r); printf("%I64d\n", f(r)-f(l-1)); } return 0;}
- codeforces 55D Beautiful numbers
- CodeForces 55D Beautiful numbers
- Codeforces 55D Beautiful numbers
- CodeForces 55D Beautiful numbers
- codeforces 55D Beautiful numbers
- codeforces 55D Beautiful numbers
- CodeForces 55D Beautiful numbers
- CodeForces 55D - Beautiful numbers
- CodeForces 55D-Beautiful numbers
- Codeforces 55D - Beautiful numbers
- CodeForces 55D Beautiful numbers
- CodeForces 55D Beautiful numbers
- 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
- HDU 3652 —— B-number
- poj3277 City Horizon(矩形面积并)
- HDU 3709 —— Balanced Number
- TestNG参数化测试之Excel读取数据
- SPOJ BALNUM - Balanced Numbers
- CodeForces 55D - Beautiful numbers
- C++primer
- HDU 4507 —— 吉哥系列故事――恨7不成妻
- POJ 1061 —— 青蛙的约会
- 利用Arcpy发布地图服务,制作切片
- ZOJ - 3609 —— Modular Inverse 【乘法逆,扩展欧几里得】
- POJ - 3286 - How many 0s? 【数位DP】
- hiho 1033 —— 交错和 【数位DP】
- hiho 1290 —— Demo Day 【DP】