Codeforces 55D Beautiful Number
A positive integer number is beautiful if and only if it's divisible by each of its nonzero digits.
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).
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 Test (s)Input
1
1 9
Output
9
Input
1
12 15
Output
2
Ideas:
- In the MoD, there is a rule, x%a = x (b*a)%a; <=> x (LCM (,..., 9) = 2520)%LCM (d[i]) = = 0; You can drop the value directly within 2520;
- at the same time a mod number, also need to record is the LCM (D[i]), so that every time the calculation of the sub-structure is added directly (the value after the MoD and the same number of LCM (can be seen as a number), LCM Total only 48, (2^3,3^ 2,5,7 combination of 4*3*2*2);
- DP[I][J][K]: [i]: High is the first position, [j]: The value after mod 2520, [K]: Record the high-level LCM, because directly to the MLE, so discretized (using the label index[]);
Code ideas refer to:Ac_von
#include <bits/stdc++.h>using namespacestd;#defineRep (i,n) for (int (i) = 0;i < (n); i++)#defineMS0 (a) memset (A,0,sizeof (a))#defineMS1 (a) memset (A,-1,sizeof (a))typedefLong Longll;Const intMOD =2520; ll dp[ +][2520][ -];intd[ +],index[mod+5];voidinit () { for(inti =1, tot =0; I <= mod;i++) if(MOD% i = =0) Index[i] = tot++; MS1 (DP);}intLcmintAintb) { returnA/__GCD (A, b) *b;} ll Dfs (intPosintPrevintPRELCM,intEdge) { if(pos = =-1)returnPrev% PRELCM?0:1;// ***ll ans =DP[POS][PREV][INDEX[PRELCM]]; if(!edge && ~ans)returnans; Ans=0; intE = Edge? D[pos]:9; for(inti =0; I <= e;i++){ intNOWLCM = I?LCM (prelcm,i): PRELCM; intNOWV = (prev *Ten+ i)%MOD; Ans+ = DFS (pos-1, Nowv,nowlcm,edge && i = =e); } if(!edge) DP[POS][PREV][INDEX[PRELCM]] =ans; returnans;} ll query (ll N) {MS0 (d);inttot =0; while(n) {D[tot+ +] = n%Ten; N/=Ten; } returnDFS (TOT-1,0,1,1);}intMain () {init (); intT; CIN>>T; while(t--) {ll l,r; scanf ("%i64d%i64d",&l,&R); printf ("%i64d\n", query (r)-Query (l1)); }}
View Code
Codeforces 55D Beautiful Number