Codeforces 55D Beautiful Number

Source: Internet
Author: User

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≤ lirI ≤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:

    1. 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;
    2. 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);
    3. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.