Codeforces--55d--beautiful numbers (digital dp,dfs+ memory)

Source: Internet
Author: User

Beautiful numbersTime limit:4000MS Memory Limit:262144KB 64bit IO for Mat:%i64d &%i64u Submit Status

Description

Volodya is a odd boy and his taste are strange as well. It seems to him, a positive integer number is beautiful if and only if it's divisible by E Ach 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-natural numbers   l i   and   R i   ( 1?≤? L i ? ≤? R i ? ≤?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 b Eautiful numbers in given intervals (from li to R i, inclusively).

Sample Input

Input
11 9
Output
9
Input
112 15
Output
2

Test instructions: L,r the number of X-multiplication LCM (x) in the range to be obtained.

The number of numbers within 1 to n is calculated first.

First, if the two-digit number is 1 to 9, and the remainder corresponds to the same, then the two numbers are appended with any of the same numbers, and the two numbers are the same as the remainder of the LCM. 1 to 9 of least common multiple 2520, so when several numbers can be the same as the results of 2520, and these numbers are composed of the same number, then these numbers can be divided into a class, after they add any of the same number, to their own LCM residual, still the same.

DP[I][256][2520], which represents the current number with I bit, where 256 is the state after the compression of 2 to 9, and then the result of the remainder of 2520.

In this way, the digital DP

#include <cstdio> #include <cstring> #include <algorithm>using namespace std; #define LL __int64ll dp[    20][256][2520], digit[20], cnt; int f (int k,int mods) {int i; for (i = 0; I <= 7; i++) {if (k& (1<<i)) {if (mods% (i+2)! = 0) b        Reak;    }} if (I <= 7) return 0; return 1;} LL dfs (int cnt,int pre,int mods,int maxd,int zero) {if (cnt = = 0) return!mods | |    (f (pre,mods));    if (maxd && zero && dp[cnt][pre][mods]! =-1) return dp[cnt][pre][mods]; LL I, r = Maxd?    9:DIGIT[CNT], ans = 0, k; for (i = 0; I <= r; i++) {if (I >= 2 && (pre& (1<< (i-2))) ==0) {An s + = DFS (cnt-1,pre| ( 1<< (I-2)), (mods*10+i)%2520,maxd| | i<r,zero| |        i); } else ans + = DFS (Cnt-1,pre, (mods*10+i)%2520,maxd| | i<r,zero| |    i); } if (Maxd && zero) dp[cnt][pre][mods] = ans;    return ans;}    ll solve (ll temp) {memset (digit,0,sizeof (digit));    CNT = 0;        while (temp) {digit[++cnt] = temp%10;    Temp/= 10; } return Dfs (cnt,0,0,0,0);}    int main () {int t;    LL N, M;    Memset (Dp,-1,sizeof (DP));    scanf ("%d", &t);        while (t--) {scanf ("%i64d%i64d", &n, &m);    printf ("%i64d\n", Solve (m)-solve (n-1)); } return 0;}

Codeforces--55d--beautiful numbers (digital dp,dfs+ memory)

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.