[暑假集训--数位dp]cf55D Beautiful numbers
Posted zhber
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[暑假集训--数位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.
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 should contain t numbers — answers to the queries, one number per line — quantities of beautiful numbers in given intervals (from li to ri, inclusively).
1
1 9
9
1
12 15
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 }
以上是关于[暑假集训--数位dp]cf55D Beautiful numbers的主要内容,如果未能解决你的问题,请参考以下文章
CF55D Beautiful numbers (数位dp)
[CF55D]Beautiful numbers(数位dp,状态压缩)
[暑假集训--数位dp]LightOj1205 Palindromic Numbers