#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int MOD=2520;
int ch[10][4];
vector<pair<int,int>> xd;
long long dp[2][2][20][MOD+10][4][3][2][2];//t,type(0-do 9,1-do x),cyfra,mod,2,3,5,7
inline long long solve(long long x,bool t)
{
int i,j,k,l,m,it=1;
long long ans=0;
dp[t][0][0][0][0][0][0][0]=dp[t][1][0][0][0][0][0][0]=1;
for(int pot=1;x>0;x/=10,pot=(pot*10)%MOD,it++)
{
for(int c=1;c<=9;c++)
{
if(c==(x%10))
{
for(m=0;m<MOD;m++)
{
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<2;k++)
{
for(l=0;l<2;l++)
dp[t][1][it][m][i][j][k][l]=dp[t][0][it][m][i][j][k][l];
}
}
}
}
for(m=0;m<MOD;m++)
{
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<2;k++)
{
for(l=0;l<2;l++)
dp[t][1][it][(m+c*pot)%MOD][max(i,ch[c][0])][max(j,ch[c][1])][(k|ch[c][2])][(l|ch[c][3])]+=dp[t][1][it-1][m][i][j][k][l];
}
}
}
}
}
for(m=0;m<MOD;m++)
{
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<2;k++)
{
for(l=0;l<2;l++)
dp[t][0][it][(m+c*pot)%MOD][max(i,ch[c][0])][max(j,ch[c][1])][(k|ch[c][2])][(l|ch[c][3])]+=dp[t][0][it-1][m][i][j][k][l];
}
}
}
}
}
int p=1,tt=(x<10 ? 1:0);
for(m=0;m<MOD;m++)
{
for(i=0;i<4;i++,p*=2)
{
for(j=0;j<3;j++,p*=3)
{
for(k=0;k<2;k++,p*=5)
{
for(l=0;l<2;l++,p*=7)
{
if(m%p==0)
ans+=dp[t][tt][it][m][i][j][k][l];
}
p/=49;
}
p/=25;
}
p/=27;
}
p/=16;
}
}
return ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long l,r;
cin>>l>>r;
ch[2][0]=1;
ch[3][1]=1;
ch[4][0]=2;
ch[5][2]=1;
ch[6][0]=1;ch[6][1]=1;
ch[7][3]=1;
ch[8][0]=3;
ch[9][1]=2;
//cerr<<solve(r,0)<<" "<<solve(l,1)<<"\n";
cout<<solve(r,0)-solve(l-1,1)<<"\n";
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 | #include<bits/stdc++.h> #define fi first #define se second using namespace std; const int MOD=2520; int ch[10][4]; vector<pair<int,int>> xd; long long dp[2][2][20][MOD+10][4][3][2][2];//t,type(0-do 9,1-do x),cyfra,mod,2,3,5,7 inline long long solve(long long x,bool t) { int i,j,k,l,m,it=1; long long ans=0; dp[t][0][0][0][0][0][0][0]=dp[t][1][0][0][0][0][0][0]=1; for(int pot=1;x>0;x/=10,pot=(pot*10)%MOD,it++) { for(int c=1;c<=9;c++) { if(c==(x%10)) { for(m=0;m<MOD;m++) { for(i=0;i<4;i++) { for(j=0;j<3;j++) { for(k=0;k<2;k++) { for(l=0;l<2;l++) dp[t][1][it][m][i][j][k][l]=dp[t][0][it][m][i][j][k][l]; } } } } for(m=0;m<MOD;m++) { for(i=0;i<4;i++) { for(j=0;j<3;j++) { for(k=0;k<2;k++) { for(l=0;l<2;l++) dp[t][1][it][(m+c*pot)%MOD][max(i,ch[c][0])][max(j,ch[c][1])][(k|ch[c][2])][(l|ch[c][3])]+=dp[t][1][it-1][m][i][j][k][l]; } } } } } for(m=0;m<MOD;m++) { for(i=0;i<4;i++) { for(j=0;j<3;j++) { for(k=0;k<2;k++) { for(l=0;l<2;l++) dp[t][0][it][(m+c*pot)%MOD][max(i,ch[c][0])][max(j,ch[c][1])][(k|ch[c][2])][(l|ch[c][3])]+=dp[t][0][it-1][m][i][j][k][l]; } } } } } int p=1,tt=(x<10 ? 1:0); for(m=0;m<MOD;m++) { for(i=0;i<4;i++,p*=2) { for(j=0;j<3;j++,p*=3) { for(k=0;k<2;k++,p*=5) { for(l=0;l<2;l++,p*=7) { if(m%p==0) ans+=dp[t][tt][it][m][i][j][k][l]; } p/=49; } p/=25; } p/=27; } p/=16; } } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long l,r; cin>>l>>r; ch[2][0]=1; ch[3][1]=1; ch[4][0]=2; ch[5][2]=1; ch[6][0]=1;ch[6][1]=1; ch[7][3]=1; ch[8][0]=3; ch[9][1]=2; //cerr<<solve(r,0)<<" "<<solve(l,1)<<"\n"; cout<<solve(r,0)-solve(l-1,1)<<"\n"; return 0; } |
English