#include <bits/stdc++.h>
using namespace std;
#define st first
#define nd second
#define pb push_back
#define sz(x) (int)(x).size()
#define ll long long
int mod=1000000007;
int inf=1000000007;
ll infl=1000000000000000007;
int lc[1007];
ll DP[20][260][2600];
int pot[20];
ll pot1[20];
int pot2[20];
ll calc(ll r)
{
ll ans=0;
ll d=0;
for(int i=0; i<=18; i++) if(r/pot1[i]) d=i;
for(int i=1; i<=d; i++)
{
for(int m=0; m<pot2[9]; m++)
{
for(int a=0; a<mod; a++)
{
if(a%lc[m]==0)
{
ans+=DP[i][m][a];
}
}
}
}
int amod=0,amsk=0;
for(int i=d; i>0; i--)
{
ll x=r/pot1[i];
if(x==0) return ans;
for(int j=x-1; j>0; j--)
{
for(int m=0; m<pot2[9]; m++)
{
for(int a=0; a<mod; a++)
{
int mod1=(amod+j*pot[i]+a)%mod,msk1=(amsk|pot2[j-1])|m;
if(mod1%lc[msk1]==0)
{
ans+=DP[i][m][a];
}
}
}
}
amod=(amod+x*pot[i])%mod;
amsk|=pot2[x-1];
r%=pot1[i];
}
ll x=r/pot1[0];
for(int j=x-1; j>0; j--)
{
ll mod1=(amod+j*pot[0])%mod,msk1=(amsk|pot2[j-1]);
if(mod1%lc[msk1]==0) ans++;
}
return ans;
}
int main()
{
mod=2520;
pot[0]=1;
pot1[0]=1;
for(int i=1;i<19;i++)
{
pot[i]=(pot[i-1]*10)%mod;
pot1[i]=(pot1[i-1]*10);
}
pot2[1]=1;
for(int i=2;i<15;i++) pot2[i]=pot2[i-1]*2;
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll l,r;
cin>>l>>r;
r++;
lc[0]=1;
for(int m=1;m<pot2[9];m++)
{
lc[m]=1;
for(int j=1;j<10;j++)
{
if((m&pot2[j-1])) lc[m]*=j/__gcd(j,lc[m]);
}
}
for(int i=1;i<10;i++)
{
DP[1][pot2[i-1]][i]++;
}
for(int i=1;i<18;i++)
{
for(int m=0;m<pot2[9];m++)
{
for(int a=0;a<mod;a++)
{
for(int n=1; n<10; n++)
{
DP[i+1][m|pot2[n-1]][(a+n*pot[i])%mod]+=DP[i][m][a];
}
}
}
}
cout<<calc(r)-calc(l);
return 0;
}
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | #include <bits/stdc++.h> using namespace std; #define st first #define nd second #define pb push_back #define sz(x) (int)(x).size() #define ll long long int mod=1000000007; int inf=1000000007; ll infl=1000000000000000007; int lc[1007]; ll DP[20][260][2600]; int pot[20]; ll pot1[20]; int pot2[20]; ll calc(ll r) { ll ans=0; ll d=0; for(int i=0; i<=18; i++) if(r/pot1[i]) d=i; for(int i=1; i<=d; i++) { for(int m=0; m<pot2[9]; m++) { for(int a=0; a<mod; a++) { if(a%lc[m]==0) { ans+=DP[i][m][a]; } } } } int amod=0,amsk=0; for(int i=d; i>0; i--) { ll x=r/pot1[i]; if(x==0) return ans; for(int j=x-1; j>0; j--) { for(int m=0; m<pot2[9]; m++) { for(int a=0; a<mod; a++) { int mod1=(amod+j*pot[i]+a)%mod,msk1=(amsk|pot2[j-1])|m; if(mod1%lc[msk1]==0) { ans+=DP[i][m][a]; } } } } amod=(amod+x*pot[i])%mod; amsk|=pot2[x-1]; r%=pot1[i]; } ll x=r/pot1[0]; for(int j=x-1; j>0; j--) { ll mod1=(amod+j*pot[0])%mod,msk1=(amsk|pot2[j-1]); if(mod1%lc[msk1]==0) ans++; } return ans; } int main() { mod=2520; pot[0]=1; pot1[0]=1; for(int i=1;i<19;i++) { pot[i]=(pot[i-1]*10)%mod; pot1[i]=(pot1[i-1]*10); } pot2[1]=1; for(int i=2;i<15;i++) pot2[i]=pot2[i-1]*2; ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll l,r; cin>>l>>r; r++; lc[0]=1; for(int m=1;m<pot2[9];m++) { lc[m]=1; for(int j=1;j<10;j++) { if((m&pot2[j-1])) lc[m]*=j/__gcd(j,lc[m]); } } for(int i=1;i<10;i++) { DP[1][pot2[i-1]][i]++; } for(int i=1;i<18;i++) { for(int m=0;m<pot2[9];m++) { for(int a=0;a<mod;a++) { for(int n=1; n<10; n++) { DP[i+1][m|pot2[n-1]][(a+n*pot[i])%mod]+=DP[i][m][a]; } } } } cout<<calc(r)-calc(l); return 0; } |
English