// Artur Kraska, II UWr #include <bits/stdc++.h> #define forr(i, n) for(int i=0; i<n; i++) #define FOREACH(iter, coll) for(auto iter = coll.begin(); iter != coll.end(); ++iter) #define FOREACHR(iter, coll) for(auto iter = coll.rbegin(); iter != coll.rend(); ++iter) #define lbound(P,R,PRED) ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;}) #define testy() int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests) #define CLEAR(tab) memset(tab, 0, sizeof(tab)) #define CONTAIN(el, coll) (coll.find(el) != coll.end()) #define FOR(i, a, b) for(int i=a; i<=b; i++) #define FORD(i, a, b) for(int i=a; i>=b; i--) #define MP make_pair #define PB push_back #define ff first #define ss second #define deb(X) X; #define M 1000000007 #define INF 1000000007LL #define R 2520 #define ML 256 using namespace std; long long dp[2][ML][R], eq[2][ML][R]; string toBin(int x) { string res = ""; forr(i, 8) { res += '0'+x%2; x/=2; } return res; } int nwd(int a, int b) { if(b == 0) return a; return nwd(b, a%b); } int nww(int a, int b) { return a*b/nwd(a, b); } long long policz(long long x) { long long res = 0; forr(m, ML) forr(r, R) { dp[1][m][r] = eq[1][m][r] = 0; } dp[1][0][0] = 1; eq[1][0][0] = 1; int rr = 1; while(x > 0) { forr(m, ML) forr(r, R) { dp[0][m][r] = dp[1][m][r]; dp[1][m][r] = 0; eq[0][m][r] = eq[1][m][r]; eq[1][m][r] = 0; } int d = x%10; x /= 10; FOR(i, 1, 9) { int mask = 1<<i>>2; int rest = rr*i%R; forr(m, ML) forr(r, R) { dp[1][m|mask][(rest+r)%R] += dp[0][m][r]; if(d > i) eq[1][m|mask][(rest+r)%R] += dp[0][m][r]; else if(d == i) eq[1][m|mask][(rest+r)%R] += eq[0][m][r]; } } rr = rr*10%R; /* forr(m, ML) forr(r, R) { if(dp[1][m][r] > 0 || eq[1][m][r] > 0) cout << "reszta " << r << " na masce " << toBin(m) << " daje dp: " << dp[1][m][r] << " i eq: " << eq[1][m][r] << endl; } */ forr(m, ML) { int wiel = 1; FOR(i, 2, 9) if((m&(1<<(i-2))) != 0) wiel = nww(wiel, i); for(int r=0; r<R; r+=wiel) { long long ile = (x == 0 ? eq[1][m][r] : dp[1][m][r]); // if(ile > 0) // cout << "dodaje reszte " << r << " na masce " << toBin(m) << ": " << ile << endl; res += ile; } } // cout << "res: " << res << endl; } return res; } int main() { long long l, r; scanf("%lld %lld", &l, &r); printf("%lld\n", policz(r) - policz(l-1)); 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 | // Artur Kraska, II UWr #include <bits/stdc++.h> #define forr(i, n) for(int i=0; i<n; i++) #define FOREACH(iter, coll) for(auto iter = coll.begin(); iter != coll.end(); ++iter) #define FOREACHR(iter, coll) for(auto iter = coll.rbegin(); iter != coll.rend(); ++iter) #define lbound(P,R,PRED) ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;}) #define testy() int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests) #define CLEAR(tab) memset(tab, 0, sizeof(tab)) #define CONTAIN(el, coll) (coll.find(el) != coll.end()) #define FOR(i, a, b) for(int i=a; i<=b; i++) #define FORD(i, a, b) for(int i=a; i>=b; i--) #define MP make_pair #define PB push_back #define ff first #define ss second #define deb(X) X; #define M 1000000007 #define INF 1000000007LL #define R 2520 #define ML 256 using namespace std; long long dp[2][ML][R], eq[2][ML][R]; string toBin(int x) { string res = ""; forr(i, 8) { res += '0'+x%2; x/=2; } return res; } int nwd(int a, int b) { if(b == 0) return a; return nwd(b, a%b); } int nww(int a, int b) { return a*b/nwd(a, b); } long long policz(long long x) { long long res = 0; forr(m, ML) forr(r, R) { dp[1][m][r] = eq[1][m][r] = 0; } dp[1][0][0] = 1; eq[1][0][0] = 1; int rr = 1; while(x > 0) { forr(m, ML) forr(r, R) { dp[0][m][r] = dp[1][m][r]; dp[1][m][r] = 0; eq[0][m][r] = eq[1][m][r]; eq[1][m][r] = 0; } int d = x%10; x /= 10; FOR(i, 1, 9) { int mask = 1<<i>>2; int rest = rr*i%R; forr(m, ML) forr(r, R) { dp[1][m|mask][(rest+r)%R] += dp[0][m][r]; if(d > i) eq[1][m|mask][(rest+r)%R] += dp[0][m][r]; else if(d == i) eq[1][m|mask][(rest+r)%R] += eq[0][m][r]; } } rr = rr*10%R; /* forr(m, ML) forr(r, R) { if(dp[1][m][r] > 0 || eq[1][m][r] > 0) cout << "reszta " << r << " na masce " << toBin(m) << " daje dp: " << dp[1][m][r] << " i eq: " << eq[1][m][r] << endl; } */ forr(m, ML) { int wiel = 1; FOR(i, 2, 9) if((m&(1<<(i-2))) != 0) wiel = nww(wiel, i); for(int r=0; r<R; r+=wiel) { long long ile = (x == 0 ? eq[1][m][r] : dp[1][m][r]); // if(ile > 0) // cout << "dodaje reszte " << r << " na masce " << toBin(m) << ": " << ile << endl; res += ile; } } // cout << "res: " << res << endl; } return res; } int main() { long long l, r; scanf("%lld %lld", &l, &r); printf("%lld\n", policz(r) - policz(l-1)); return 0; } |