Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- using namespace std;
- int t;
- unsigned long long int A,B,dp[20][49][5][7][8][9];
- bool visit[20][49][5][7][8][9];
- int num[49] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 18, 20, 21, 24, 28, 30, 35, 36, 40, 42, 45, 56, 60, 63, 70, 72, 84, 90, 105, 120, 126, 140, 168, 180, 210, 252, 280, 315, 360, 420, 504, 630, 840, 1260, 2520};
- int gcd(int a,int b){
- if(a == 0) return b;
- return gcd(b%a,a);
- }
- unsigned long long int cal(unsigned long long int prefix,int N,int lcm,int five,int seven,int eight,int nine){
- unsigned long long int mini = prefix,maxi = prefix,tmp = 0;
- for(int i=0;i<N;++i){
- mini = mini*10llu;
- maxi = maxi*10llu+9llu;
- }
- if(mini > B || maxi < A) return 0;
- bool memo = (A <= mini && maxi <= B);
- int x;
- if(memo && visit[N][lcm][five][seven][eight][nine] == 1) return dp[N][lcm][five][seven][eight][nine];
- if(N == 0){
- if(prefix == 0) tmp = 1;
- else{
- if(
- (num[lcm]/2*2 != num[lcm] || eight/2*2 == eight) &&
- (num[lcm]/3*3 != num[lcm] || nine/3*3 == nine) &&
- (num[lcm]/4*4 != num[lcm] || eight/4*4 == eight) &&
- (num[lcm]/5*5 != num[lcm] || five/5*5 == five) &&
- (num[lcm]/6*6 != num[lcm] || (eight/2*2 == eight && nine/3*3 == nine)) &&
- (num[lcm]/7*7 != num[lcm] || seven/7*7 == seven) &&
- (num[lcm]/8*8 != num[lcm] || eight/8*8 == eight) &&
- (num[lcm]/9*9 != num[lcm] || nine/9*9 == nine)) tmp = 1;
- }
- }
- else{
- mini = prefix*10llu;
- for(int i=0;i<10;++i){
- maxi = mini + (long long int)(i);
- if(i == 0) x = lcm;
- else if(lcm == 0 && i > 0) x = lower_bound(num,num+49,i)-num;
- else x = lower_bound(num,num+49,num[lcm]*i/gcd(min(num[lcm],i),max(num[lcm],i)))-num;
- tmp += cal(maxi,N-1,x,(10*five+i)-(10*five+i)/5*5,(10*seven+i)-(10*seven+i)/7*7,(10*eight+i)-(10*eight+i)/8*8,(10*nine+i)-(10*nine+i)/9*9);
- }
- }
- if(memo){
- dp[N][lcm][five][seven][eight][nine] = tmp;
- visit[N][lcm][five][seven][eight][nine] = 1;
- }
- return tmp;
- }
- int main(){
- scanf("%d",&t);
- for(int a=0;a<t;++a){
- // memset(visit,0,sizeof(visit));
- scanf("%I64u%I64u",&A,&B);
- printf("%I64u\n",cal(0,19,0,0,0,0,0));
- // scanf("%llu%llu",&A,&B);
- // printf("%llu\n",cal(0,19,0,0,0,0,0));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement