#include<iostream> #include<stdio.h> #include<cstring> using namespace std; char num[20]; long long dp[20][2527][60]; int Lcm[2527]; long long gcd(long long a,long long b){ if(b==0) return a; return gcd(b,a%b); } long long dfs(int idx, int mod, int lcm,int tight, int non_zero){ if(idx<0) return (mod%lcm == 0); if(!tight && ~dp[idx][mod][Lcm[lcm]]) return dp[idx][mod][Lcm[lcm]]; int limit = tight?num[idx]:9; long long ret = 0; for(int i=non_zero;i<=limit;i++) ret+=dfs(idx-1,(mod*10+i)%2520,i!=0?i*lcm/gcd(i,lcm):lcm,tight&&i==limit,non_zero||(i!=0)); if(!tight) dp[idx][mod][Lcm[lcm]]=ret; return ret; } long long solve(long long x){ int n = 0; while(x) { num[n++]=x%10; x/=10; } return dfs(n-1,0,1,1,0); } int main(){ for(int i=1,j=0;i<=2520;i++) if(2520%i==0) Lcm[i]=j++; memset(dp,-1,sizeof(dp)); long long l ,r; cin >> l >> r; cout << solve(r)-solve(l-1); }
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 | #include<iostream> #include<stdio.h> #include<cstring> using namespace std; char num[20]; long long dp[20][2527][60]; int Lcm[2527]; long long gcd(long long a,long long b){ if(b==0) return a; return gcd(b,a%b); } long long dfs(int idx, int mod, int lcm,int tight, int non_zero){ if(idx<0) return (mod%lcm == 0); if(!tight && ~dp[idx][mod][Lcm[lcm]]) return dp[idx][mod][Lcm[lcm]]; int limit = tight?num[idx]:9; long long ret = 0; for(int i=non_zero;i<=limit;i++) ret+=dfs(idx-1,(mod*10+i)%2520,i!=0?i*lcm/gcd(i,lcm):lcm,tight&&i==limit,non_zero||(i!=0)); if(!tight) dp[idx][mod][Lcm[lcm]]=ret; return ret; } long long solve(long long x){ int n = 0; while(x) { num[n++]=x%10; x/=10; } return dfs(n-1,0,1,1,0); } int main(){ for(int i=1,j=0;i<=2520;i++) if(2520%i==0) Lcm[i]=j++; memset(dp,-1,sizeof(dp)); long long l ,r; cin >> l >> r; cout << solve(r)-solve(l-1); } |