#pragma GCC optimize("O3") #define _USE_MATH_DEFINES #include <bits/stdc++.h> #define BOOST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define FOR(a, b, c) for(int a = b; a < c; ++a) #define PB push_back #define MP make_pair #define INF (int)1e9+7 #define LLINF 2e18+7 #define ALL(a) a.begin(), a.end() #define SIZE(a) (int)a.size() typedef unsigned long long ULL; typedef long long LL; typedef long double LD; using namespace std; //#define DEBUG LL DP[512][2520][2]; LL DPtemp[512][2520][2]; const LL MOD = 2520; void clr(LL dp[512][2520][2]) { FOR(i, 0, 512) FOR(j, 0, 2520) FOR(z, 0, 2) dp[i][j][z] = 0; } LL maskLCM(LL mask) { vector <LL> numbers; for(int i = 0; i < 9; ++i) { if(mask & (1ll << i)) { numbers.push_back(i + 1); } } if(numbers.size() == 0) return 1; LL lcm = numbers[0]; for(int i = 1; i < numbers.size(); ++i) { lcm = lcm * numbers[i] / __gcd(lcm, numbers[i]); } return lcm; } LL Solve(LL n) { clr(DP); LL res = 0; LL pow10 = 1; DP[0][0][1] = 1; while(n) { clr(DPtemp); LL d = n % 10; n /= 10; for(LL z = 1; z <= 9; ++z) { LL x = (pow10 * z) % MOD; FOR(mask, 0, 512) { FOR(r, 0, 2520) { LL newmask = mask | (1ll << (z - 1)); LL newr = (r + x) % MOD; if(z < d) { DPtemp[newmask][newr][1] += DP[mask][r][1] + DP[mask][r][0]; } else if(z == d) { DPtemp[newmask][newr][1] += DP[mask][r][1]; DPtemp[newmask][newr][0] += DP[mask][r][0]; } else { DPtemp[newmask][newr][0] += DP[mask][r][0] + DP[mask][r][1]; } } } } FOR(i, 0, 512) FOR(j, 0, 2520) FOR(z, 0, 2) DP[i][j][z] = DPtemp[i][j][z]; //update result FOR(mask, 0, 512) { LL lcm = maskLCM(mask); for(LL r = 0; r < MOD; r += lcm) { if(n) { res += DP[mask][r][0] + DP[mask][r][1]; } else { res += DP[mask][r][1]; } } } pow10 = (pow10 * 10) % MOD; } return res; } int main() { #ifndef DEBUG BOOST; #endif LL a, b; cin >> a >> b; cout << Solve(b) - Solve(a - 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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | #pragma GCC optimize("O3") #define _USE_MATH_DEFINES #include <bits/stdc++.h> #define BOOST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define FOR(a, b, c) for(int a = b; a < c; ++a) #define PB push_back #define MP make_pair #define INF (int)1e9+7 #define LLINF 2e18+7 #define ALL(a) a.begin(), a.end() #define SIZE(a) (int)a.size() typedef unsigned long long ULL; typedef long long LL; typedef long double LD; using namespace std; //#define DEBUG LL DP[512][2520][2]; LL DPtemp[512][2520][2]; const LL MOD = 2520; void clr(LL dp[512][2520][2]) { FOR(i, 0, 512) FOR(j, 0, 2520) FOR(z, 0, 2) dp[i][j][z] = 0; } LL maskLCM(LL mask) { vector <LL> numbers; for(int i = 0; i < 9; ++i) { if(mask & (1ll << i)) { numbers.push_back(i + 1); } } if(numbers.size() == 0) return 1; LL lcm = numbers[0]; for(int i = 1; i < numbers.size(); ++i) { lcm = lcm * numbers[i] / __gcd(lcm, numbers[i]); } return lcm; } LL Solve(LL n) { clr(DP); LL res = 0; LL pow10 = 1; DP[0][0][1] = 1; while(n) { clr(DPtemp); LL d = n % 10; n /= 10; for(LL z = 1; z <= 9; ++z) { LL x = (pow10 * z) % MOD; FOR(mask, 0, 512) { FOR(r, 0, 2520) { LL newmask = mask | (1ll << (z - 1)); LL newr = (r + x) % MOD; if(z < d) { DPtemp[newmask][newr][1] += DP[mask][r][1] + DP[mask][r][0]; } else if(z == d) { DPtemp[newmask][newr][1] += DP[mask][r][1]; DPtemp[newmask][newr][0] += DP[mask][r][0]; } else { DPtemp[newmask][newr][0] += DP[mask][r][0] + DP[mask][r][1]; } } } } FOR(i, 0, 512) FOR(j, 0, 2520) FOR(z, 0, 2) DP[i][j][z] = DPtemp[i][j][z]; //update result FOR(mask, 0, 512) { LL lcm = maskLCM(mask); for(LL r = 0; r < MOD; r += lcm) { if(n) { res += DP[mask][r][0] + DP[mask][r][1]; } else { res += DP[mask][r][1]; } } } pow10 = (pow10 * 10) % MOD; } return res; } int main() { #ifndef DEBUG BOOST; #endif LL a, b; cin >> a >> b; cout << Solve(b) - Solve(a - 1) << "\n"; return 0; } |