[暑假集训--数位dp]cf55D Beautiful numbers

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 ≤ t ≤ 10). Each of the next t lines contains two natural numbers li and ri (1 ≤ li ≤ ri ≤ 9 ·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 should contain t numbers — answers to the queries, one number per line — quantities of beautiful numbers in given intervals (from li to ri, inclusively).

Examples
Input
1
1 9
Output
9
Input
1
12 15
Output
2

 

  

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<algorithm>
 6 #include<cmath>
 7 #include<queue>
 8 #include<deque>
 9 #include<set>
10 #include<map>
11 #include<ctime>
12 #define LL long long
13 #define inf 0x7ffffff
14 #define pa pair<LL,LL>
15 #define mkp(a,b) make_pair(a,b)
16 #define pi 3.1415926535897932384626433832795028841971
17 using namespace std;
18 inline LL read()
19 {
20     LL x=0,f=1;char ch=getchar();
21     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
22     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
23     return x*f;
24 }
25 LL n,len,l,r,tot;
26 LL f[20][210][2530];
27 LL d[110];
28 LL rnk[2530];
29 LL mrk[210];
30 inline LL dfs(LL now,LL lcm,LL rem,LL fp)
31 {
32     if (now==1)return rem%lcm==0;
33     if (!fp&&f[now][rnk[lcm]][rem]!=-1)return f[now][rnk[lcm]][rem];
34     LL ans=0;
35     LL mx=fp?d[now-1]:9;
36     LL nexlcm,nexrem=rem*10%2520;
37     for (LL i=0;i<=mx;i++)
38     {
39         nexlcm=lcm;
40         if (i)nexlcm=nexlcm*i/__gcd(lcm,i);
41 
42         nexrem+=i;if (nexrem>=2520)nexrem-=2520;
43 
44         ans+=dfs(now-1,nexlcm,nexrem,fp&&i==mx);
45 
46         nexrem-=i;if (nexrem<0)nexrem+=2520;
47     }
48     if (!fp)f[now][rnk[lcm]][rem]=ans;
49     return ans;
50 }
51 inline LL calc(LL x)
52 {
53     if (x==-1)return 0;
54     if (x==0)return 1;
55     LL xxx=x;
56     len=0;
57     while (xxx)
58     {
59         d[++len]=xxx%10;
60         xxx/=10;
61     }
62     LL sum=0;
63     for (LL i=0;i<=d[len];i++)
64     {
65         sum+=dfs(len,i?i:1,i,i==d[len]);
66     }
67     return sum;
68 }
69 inline void init()
70 {
71     priority_queue<LL,vector<LL>,greater<LL> >q;
72     q.push(1);
73     while (1)
74     {
75         LL now=q.top();q.pop();
76         if (now>2520)break;
77         if (rnk[now])continue;
78         for (LL i=2;i<=10;i++)q.push(i*now);
79         rnk[now]=++tot;
80         mrk[tot]=now;
81     }
82 }
83 int main()
84 {
85     init();
86     LL T=read();
87     memset(f,-1,sizeof(f));
88     while (T--)
89     {
90         l=read();
91         r=read();
92         if (r<l)swap(l,r);
93         printf("%lld\n",calc(r)-calc(l-1));
94     }
95 }
cf 55D

 

转载于:https://www.cnblogs.com/zhber/p/7284389.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值