Codeforces Beta Round #51---D. Beautiful numbers (digital DP, ingenious)

Source: Internet
Author: User

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 digits. 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 the natural numbers Li and ri (1?≤?li?≤?ri?≤?9? 1018).

%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 numbers in given I Ntervals (from Li to Ri, inclusively).
Sample Test (s)
Input

1
1 9

Output

9

Input

1
12 15

Output

2

It's a really subtle approach.
If a number can be divisible by the number of its each, it must be divisible by the LCM of those numbers, the largest LCM is 2520, so those numbers must be divisible by a factor of 2520.
Set DP[CUR][REST][LCM] to the cur bit, the preceding number of the number of 2520 modulo to rest, the previous number of LCM for the number of LCM, directly this will be super memory, so the third dimension discretization

Why does the number of the preceding numbers have no effect on the answer after 2520 modulo?
Set this number to 2520 * k + B, the LCM is a factor of 2520, so:
(2520 * k + b)% LCM = b LCM
So it doesn't affect

And then there's the digital DP thing.
64-bit integers are obviously used

/************************************************************************* > File Name:cf55d.cpp > Author : ALex > Mail: [email protected] > Created time:2015 February 23 Monday 22:25 40 seconds ********************************** **************************************/#include <map>#include <set>#include <queue>#include <stack>#include <vector>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace STD;Const DoublePI =ACOs(-1);Const intINF =0x3f3f3f3f;Const DoubleEPS =1e-15;typedef Long LongLL;typedefPair <int,int> PLL; LL dp[ -][2600][ -];intlcm_arr[ -];inthash[ the];intCntintbit[ -]; ll GCD (ll A, ll b) {returnB? GCD (b, a% B): A;} ll LCM (ll A, ll b) {returnA/GCD (A, b) * b;} LL DFS (intCurintRestintIndBOOLFlagBOOLZero) {if(cur = =-1)    {if(zero) {return 0; }return(rest% lcm_arr[ind] = =0); }if(!flag && ~dp[cur][rest][ind]) {returnDp[cur][rest][ind]; } LL ans =0;intEnd = Flag? Bit[cur]:9; for(inti =0; I <= end; ++i) {if(Zero &&!i) {ans + = DFS (cur-1,0,0, Flag && (i = = end),1); }Else if(Zero && i) {ans + = DFS (cur-1, I, Hash[i], flag && (i = = end),0); }Else{if(i = =0) {ans + = DFS (cur-1, rest *Ten%2520, IND, flag && (i = = end), zero && (i = =0));Continue; }intLCM = LCM (Lcm_arr[ind], i); Ans + = DFS (cur-1, (REST *Ten+ i)%2520, HASH[LCM], flag && (i = = end), zero && (i = =0)); }    }if(!flag)    {Dp[cur][rest][ind] = ans; }returnAns;} ll Calc (ll N) {intRET =0; while(n) {bit[ret++] = nTen; N/=Ten; }returnDFS (RET-1,0,1,1,1);}intMain () {cnt =0;memset(DP,-1,sizeof(DP)); for(inti =1; I <=2520; ++i) {if(2520% i = =0) {lcm_arr[++cnt] = i;        Hash[i] = cnt; }} LL L, R;intTscanf("%d", &t); while(t--) {Cin>> l >> R;cout<< Calc (r)-Calc (L-1) << Endl; }return 0;}

Codeforces Beta Round #51---D. Beautiful numbers (digital DP, ingenious)

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.