Codeforces Beta Round # 51---D. Beautiful numbers (Digital dp, clever ),

Source: Internet
Author: User

Codeforces Beta Round # 51---D. Beautiful numbers (Digital dp, clever ),

Volodya is an odd boy and his taste is strange as well. it seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. we will not 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 ≤ processing t ≤ processing 10 ). each of the next t lines contains two natural numbers li and ri (1 branch ≤ physical li branch ≤ physical ri branch ≤ maximum 9 branch · 1018 ).

Please, do not use % lld specificator to read or write 64-bit integers in C ++. It is preffered to use cin (also you may use % I64d ).
Output

Output shoshould contain t numbers-answers to the queries, one number per line-quantities of beautiful numbers in given intervals (from li to ri, inclusively ).
Sample test (s)
Input

1
1 9

Output

9

Input

1
12 15

Output

2

This practice is really subtle
If a number can be divisible by the number in each bit, it can be divisible by the LCM of those numbers, and the maximum LCM is 2520, therefore, the number must be divisible by a factor of 2520.
Set dp [cur] [rest] [lcm] To the cur bit. The modulo of the first number pair 2520 is rest, the lcm of the previous number is the number of lcm, which will directly exceed the memory, so the three-dimensional discretization

Why does the number composed of the preceding number have no impact on the answer after the modulo of 2520?
Set this number to 2520 * k + B and modulo the lcm. The lcm is a factor of 2520. Therefore:
(2520 * k + B) % lcm = B % lcm
So it does not affect

Then it's about the digital dp.
It must be a 64-bit integer.

/*************************************** * *********************************> File Name: cf55d. cpp> Author: ALex> Mail: zchao1995@gmail.com> Created Time: monday, ******************************** **************************************** /# 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 double pi = acos (-1); const int inf = 0x3f3f3f; const double eps = 1e-15; typedef long LL; typedef pair <int, int> PLL; LL dp [33] [2600] [60]; int LCM_ARR [60]; int HASH [3000]; int cnt; int bit [30]; LL gcd (LL a, LL B) {return B? Gcd (B, a % B): a;} ll lcm (LL a, LL B) {return a/gcd (a, B) * B ;} LL dfs (int cur, int rest, int ind, bool flag, bool zero) {if (cur =-1) {if (zero) {return 0 ;} return (rest % LCM_ARR [ind] = 0);} if (! Flag &&~ Dp [cur] [rest] [ind]) {return dp [cur] [rest] [ind];} LL ans = 0; int end = flag? Bit [cur]: 9; for (int I = 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 * 10% 2520, ind, flag & (I = end), zero & (I = 0 )); continue;} int lcm = LCM (LCM_ARR [ind], I); ans + = dfs (cur-1, (rest * 10 + I) % 2520, HASH [lcm], flag & (I = end), zero & (I = 0) ;}} if (! Flag) {dp [cur] [rest] [ind] = ans;} return ans;} LL calc (LL n) {int ret = 0; while (n) {bit [ret ++] = n % 10; n/= 10;} return dfs (ret-1, 0, 1, 1);} int main () {cnt = 0; memset (dp,-1, sizeof (dp); for (int I = 1; I <= 2520; ++ I) {if (2520% I = 0) {LCM_ARR [++ cnt] = I; HASH [I] = cnt ;}} LL l, r; int t; scanf ("% d", & t); while (t --) {cin> l> r; cout <calc (r)-calc (l-1) <endl;} return 0 ;}

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.