#pragma GCC optimize("Ofast") #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; #define mp make_pair #define eb emplace_back #define pb push_back #define e1 first #define e2 second #define uint unsigned int #define ll long long #define ull unsigned long long #define ld long double #define float long double #define size(x) (int)x.size() #define satori int testCases; cin>>testCases; while(testCases--) #define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) #define all(r) begin(r),end(r) #define time chrono::high_resolution_clock().now().time_since_epoch().count() typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set; mt19937 rng(chrono::high_resolution_clock().now().time_since_epoch().count()); /////////////////// #define DEBUG if(1) /////////////////// const int MAXM=(1<<9),MAXLCM=5*7*8*9; ll dp[2][2][MAXM][MAXLCM]; int to[10][MAXLCM]; void prep(){ for(int i=0;i<10;i++) for(int j=0;j<MAXLCM;j++) to[i][j]=(j*10+i)%MAXLCM; } ll solve(ll n){ if(n==0) return 0; vector<int> d; while(n) d.eb(n%(10ll)),n/=(10ll); for(int j=0;j<2;j++) for(int k=0;k<MAXM;k++) fill(dp[size(d)&1][j][k],dp[size(d)&1][j][k]+MAXLCM,0); dp[size(d)&1][1][0][0]=1; for(int i=size(d);i>0;i--){ if(i!=size(d)) dp[i&1][0][0][0]++; int nx=(i-1)&1; for(int j=0;j<2;j++) for(int k=0;k<MAXM;k++) fill(dp[nx][j][k],dp[nx][j][k]+MAXLCM,0); for(int j=0;j<MAXM;j++) for(int k=0;k<MAXLCM;k++){ if(d[i-1]!=0) dp[nx][1][j|(1<<(d[i-1]-1))][to[d[i-1]][k]]+=dp[i&1][1][j][k]; for(int c=1;c<d[i-1];c++) dp[nx][0][j|(1<<(c-1))][to[c][k]]+=dp[i&1][1][j][k]; } for(int j=0;j<MAXM;j++) for(int k=0;k<MAXLCM;k++) for(int c=1;c<10;c++) dp[nx][0][j|(1<<(c-1))][to[c][k]]+=dp[i&1][0][j][k]; } ll res=0; for(int i=0;i<MAXM;i++) for(int j=0;j<MAXLCM;j++){ ll sum=dp[0][0][i][j]+dp[0][1][i][j]; bool fail=false; for(int k=0;k<9;k++) if((i&(1<<k))&&(j%(k+1))){ fail=true; break; } if(!fail) res+=sum; } return res; } int32_t main() { fastio; ll l,r; cin>>l>>r; prep(); ll res=solve(r)-solve(l-1ll); cout<<res<<'\n'; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | #pragma GCC optimize("Ofast") #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; #define mp make_pair #define eb emplace_back #define pb push_back #define e1 first #define e2 second #define uint unsigned int #define ll long long #define ull unsigned long long #define ld long double #define float long double #define size(x) (int)x.size() #define satori int testCases; cin>>testCases; while(testCases--) #define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) #define all(r) begin(r),end(r) #define time chrono::high_resolution_clock().now().time_since_epoch().count() typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set; mt19937 rng(chrono::high_resolution_clock().now().time_since_epoch().count()); /////////////////// #define DEBUG if(1) /////////////////// const int MAXM=(1<<9),MAXLCM=5*7*8*9; ll dp[2][2][MAXM][MAXLCM]; int to[10][MAXLCM]; void prep(){ for(int i=0;i<10;i++) for(int j=0;j<MAXLCM;j++) to[i][j]=(j*10+i)%MAXLCM; } ll solve(ll n){ if(n==0) return 0; vector<int> d; while(n) d.eb(n%(10ll)),n/=(10ll); for(int j=0;j<2;j++) for(int k=0;k<MAXM;k++) fill(dp[size(d)&1][j][k],dp[size(d)&1][j][k]+MAXLCM,0); dp[size(d)&1][1][0][0]=1; for(int i=size(d);i>0;i--){ if(i!=size(d)) dp[i&1][0][0][0]++; int nx=(i-1)&1; for(int j=0;j<2;j++) for(int k=0;k<MAXM;k++) fill(dp[nx][j][k],dp[nx][j][k]+MAXLCM,0); for(int j=0;j<MAXM;j++) for(int k=0;k<MAXLCM;k++){ if(d[i-1]!=0) dp[nx][1][j|(1<<(d[i-1]-1))][to[d[i-1]][k]]+=dp[i&1][1][j][k]; for(int c=1;c<d[i-1];c++) dp[nx][0][j|(1<<(c-1))][to[c][k]]+=dp[i&1][1][j][k]; } for(int j=0;j<MAXM;j++) for(int k=0;k<MAXLCM;k++) for(int c=1;c<10;c++) dp[nx][0][j|(1<<(c-1))][to[c][k]]+=dp[i&1][0][j][k]; } ll res=0; for(int i=0;i<MAXM;i++) for(int j=0;j<MAXLCM;j++){ ll sum=dp[0][0][i][j]+dp[0][1][i][j]; bool fail=false; for(int k=0;k<9;k++) if((i&(1<<k))&&(j%(k+1))){ fail=true; break; } if(!fail) res+=sum; } return res; } int32_t main() { fastio; ll l,r; cin>>l>>r; prep(); ll res=solve(r)-solve(l-1ll); cout<<res<<'\n'; } |