/* * author: pavveu */ #include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vll = vector<ll>; using pii = pair<int,int>; using graph = vector<vi>; #define FOR(name__, upper__) for (int name__ = 0; name__ < (upper__); ++name__) #define all(x) begin(x), end(x) #define mp make_pair #define mt make_tuple template<class T> void initialize_matrix(vector<vector<T>>& matrix, int rows, int cols, T value) { assert(matrix.empty()); FOR (row, rows) matrix.emplace_back(cols, value); } const int M = 2520; const int LEN = 18; const int D = 9; const int S = 1<<D; // int cnt[LEN][D][M]; ll dp[2][D][M][S]; ll cnt[LEN][D][M]; void calc() { for (int i = 0; i < D; i++) { dp[0][i][i + 1][1<<i] = 1; } for (int l = 0; l < LEN; l++) { int curr_l = l % 2; int next_l = (l + 1) % 2; for (int d = 0; d < D; d++) { for (int m = 0; m < M; m++) { int mod_flag = 0; for (int i = 0; i < D; i++) { if ( m % (i + 1) == 0 ) mod_flag |= (1<<i); } for (int s = 1; s < S; s++) { if ( dp[curr_l][d][m][s] == 0 ) continue; // s is a binary subset of mod_flag if ( (mod_flag & s) == s ) { cnt[l][d][m] += dp[curr_l][d][m][s]; } if ( l == LEN - 1 ) continue; for (int i = 0; i < D; i++ ) { int next_m = (m * 10 + i + 1) % M; int next_s = s | (1<<i); dp[next_l][d][next_m][next_s] += dp[curr_l][d][m][s]; } dp[curr_l][d][m][s] = 0; } } } } } ll nww(ll a, ll b) { return a * b / __gcd(a, b); } ll potyczkow_lessequal(ll x) { if ( x == 0 ) return 0; string s { to_string(x) }; int len = s.size(); ll sum { 0 }; FOR(l, len - 1) { FOR(d, D) FOR(m, M) sum += cnt[l][d][m]; } ll prefix = 0; vector<int> met_digits; for (int i = 0; i < len; i++) { int current_digit { s[i] - '1' }; if ( i == len - 1 ) current_digit++; FOR(d, current_digit) { FOR(m, M) { // number later must be congruent mod every digit in prefix // bool good_m = true; for (int md : met_digits) { int residue = ((-prefix % md) + md) % md; if ( residue != m % md ) { good_m = false; } } if ( good_m ) { sum += cnt[len - i - 1][d][m]; } } } prefix += (current_digit + 1) * pow(10, len - i - 1); met_digits.push_back(current_digit + 1); } return sum; } bool is_potyczkow(int a) { int x = a; while ( x > 0 ) { int c = x % 10; if ( c == 0 || a % c != 0 ) return false; x /= 10; } return true; } int main() { ios::sync_with_stdio(false); cin.tie(0); calc(); ll a, b; cin >> a >> b; cout << potyczkow_lessequal(b) - potyczkow_lessequal(a - 1) << endl; 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 145 146 147 148 149 150 151 | /* * author: pavveu */ #include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vll = vector<ll>; using pii = pair<int,int>; using graph = vector<vi>; #define FOR(name__, upper__) for (int name__ = 0; name__ < (upper__); ++name__) #define all(x) begin(x), end(x) #define mp make_pair #define mt make_tuple template<class T> void initialize_matrix(vector<vector<T>>& matrix, int rows, int cols, T value) { assert(matrix.empty()); FOR (row, rows) matrix.emplace_back(cols, value); } const int M = 2520; const int LEN = 18; const int D = 9; const int S = 1<<D; // int cnt[LEN][D][M]; ll dp[2][D][M][S]; ll cnt[LEN][D][M]; void calc() { for (int i = 0; i < D; i++) { dp[0][i][i + 1][1<<i] = 1; } for (int l = 0; l < LEN; l++) { int curr_l = l % 2; int next_l = (l + 1) % 2; for (int d = 0; d < D; d++) { for (int m = 0; m < M; m++) { int mod_flag = 0; for (int i = 0; i < D; i++) { if ( m % (i + 1) == 0 ) mod_flag |= (1<<i); } for (int s = 1; s < S; s++) { if ( dp[curr_l][d][m][s] == 0 ) continue; // s is a binary subset of mod_flag if ( (mod_flag & s) == s ) { cnt[l][d][m] += dp[curr_l][d][m][s]; } if ( l == LEN - 1 ) continue; for (int i = 0; i < D; i++ ) { int next_m = (m * 10 + i + 1) % M; int next_s = s | (1<<i); dp[next_l][d][next_m][next_s] += dp[curr_l][d][m][s]; } dp[curr_l][d][m][s] = 0; } } } } } ll nww(ll a, ll b) { return a * b / __gcd(a, b); } ll potyczkow_lessequal(ll x) { if ( x == 0 ) return 0; string s { to_string(x) }; int len = s.size(); ll sum { 0 }; FOR(l, len - 1) { FOR(d, D) FOR(m, M) sum += cnt[l][d][m]; } ll prefix = 0; vector<int> met_digits; for (int i = 0; i < len; i++) { int current_digit { s[i] - '1' }; if ( i == len - 1 ) current_digit++; FOR(d, current_digit) { FOR(m, M) { // number later must be congruent mod every digit in prefix // bool good_m = true; for (int md : met_digits) { int residue = ((-prefix % md) + md) % md; if ( residue != m % md ) { good_m = false; } } if ( good_m ) { sum += cnt[len - i - 1][d][m]; } } } prefix += (current_digit + 1) * pow(10, len - i - 1); met_digits.push_back(current_digit + 1); } return sum; } bool is_potyczkow(int a) { int x = a; while ( x > 0 ) { int c = x % 10; if ( c == 0 || a % c != 0 ) return false; x /= 10; } return true; } int main() { ios::sync_with_stdio(false); cin.tie(0); calc(); ll a, b; cin >> a >> b; cout << potyczkow_lessequal(b) - potyczkow_lessequal(a - 1) << endl; return 0; } |