Beautiful NumbersTopic links
Description
Volodya is a odd boy and his taste are strange as well. It seems to him this a positive integer number is beautiful if and only if it's divisible by each of its nonzero digi Ts. We 'll not be 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 II natural numbers li and R i (1≤ li ≤ rI ≤9 10).
%lld specificator to read or write 64-bit integers in C + +. It is the preffered to use cin (also-use %i64d).
Output
Output should contain T numbers-answers to the queries, one number per line-quantities of beautiful Numbe RS in given intervals (from li to Ri, inclusively).
Sample Input
Input
1
1 9
Output
9
Input
1
12 15
Output
2
Test instructions is to give a range of beautiful number in this range, beautiful number means that a count can be divisible by the number in each position.
Obviously, beautiful number itself must be divisible by the least common multiple of each of you. The least common multiple species of 0~9 are actually few and small (2520). So you can use dp[20][50][2521] to save the state. DP[POS][ID][LCM], id refers to some kind of least common multiple. Since least common multiple is only 2520 maximum, num%= 2520 can be used, and the answer will be the same. Next, you'll be able to place the digital DP template.
#include <cstdio>#include<cstring>#include<algorithm>#include<iostream>using namespacestd;#definell Long Longintdig[ -];ll dp[ -][ -][2521];intid[2521];ll GETLCM (ll A, ll b) {returnA/__GCD (A, b) *b;} ll Dfs (intPOS, ll LCM, LL Sum,intLim) {if(pos = =-1)returnSum% LCM = =0; if(Dp[pos][id[lcm]][sum]! =-1&&!lim)returnDp[pos][id[lcm]][sum]; intEnd = Lim? Dig[pos]:9; LL ret=0; for(inti =0; I <= End; i++) {ll NLCM=LCM; if(i) NLCM =GETLCM (NLCM, i); ll Nsum= Sum *Ten+i; if(POS) Nsum%=2520; RET+ = DFS (pos-1, NLCM, nsum, i = = End &&Lim); } if(!lim) Dp[pos][id[lcm]][sum] =ret; returnret;} ll Func (ll a) {intn =0; while(a) {dig[n+ +] = a%Ten; A/=Ten; } returnDFS (N-1,1,0,1);}voidinit () {intTMP =0; for(inti =1; I <=2520; i++)if(2520% i = =0) Id[i] = tmp++; Memset (DP,-1,sizeof(DP));}intMain () {intT; Init (); scanf ("%d", &t); while(t--) {ll A, B; scanf ("%i64d%i64d", &a, &b); printf ("%i64d\n", func (b)-Func (A-1)); }}
Codeforces 55D Beautiful Numbers