//Konrad Paluszek,University of Warsaw(former XIV LO Staszic) //#STAY AT HOME #ifndef LOCAL #pragma GCC optimize("O3") #endif #define TIME (chrono::steady_clock::now().time_since_epoch().count()) #include<bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> #define xfm(a,b)a##b #define xwb(a,b)xfm(a,b) #define _ xwb(nvj,__LINE__) #define __ xwb(kjy,__LINE__) #define ___ xwb(cjm,__LINE__) #define REP(i,n)for(urs(n)i=0;i<(n);++i) #define PER(r...)for(bool _=1;_||next_permutation(r);_=false) #define FS(r)r.first,r.second #define M0(r) memset(r, 0, sizeof(r)) #define sim template<class c #define ros return o #define rans return ans #define forbits(i,m)if(m)for(urs(m)i=ctz(m),i##nvj=m;i##nvj;i##nvj^=((urs(m))1<<i),i=i##nvj?ctz(i##nvj):0) #define fordbits(i,m)if(m)for(urs(m)i=8*sizeof(m)-clz(m)-1,i##nxd=m;i##nxd;i##nxd^=((urs(m))1<<i),i=i##nxd?8*sizeof(m)-clz(i##nxd)-1:0) #define ksets(t, m, k, n) for (t m = (((t)1 << (k)) - 1); m < ((t)1 << (n)); m = nux(m)) #define urs(r...)typename decay<decltype(r)>::type #define hur(f,g,r)sim>int f(c a){if(sizeof(c)==16)return r;if(sizeof(c)==8)return g##ll(a);return g(a);} #define pwq(t,i) int clz(t x){return clz<int>(x)-i;} using namespace __gnu_pbds;using namespace std;using ll=long long;using ld=long double;using ull=unsigned long long;using vi=vector<int>;using vll=vector<ll>;using pii=pair<int,int>;using pll=pair<ll,ll>;using vpii=vector<pii>;using spii=set<pii>;using mii=map<int,int>;using unt=unsigned int;sim>using min_queue=priority_queue<c,vector<c>,greater<c>>;sim,class b,class cmp=less<c> >using ordered_map=tree<c,b,cmp,rb_tree_tag,tree_order_statistics_node_update>;sim, class cmp=less<c>>using ordered_set = ordered_map<c,null_type,cmp>;hur(popc,__builtin_popcount,popc<ull>(a)+popc<ull>(a>>64))hur(ctz,__builtin_ctz,(ull)a?ctz<ull>(a):64+ctz<ull>(a>>64))hur(clz,__builtin_clz,a>>64?clz<ull>(a>>64):64+clz<ull>(a))pwq(short,16)pwq(uint16_t,16)pwq(char,24)pwq(int8_t,24)pwq(uint8_t,24)sim,class N>bool mini(c&o,const N&h){if(o>h)ros=h,1;return 0;}sim,class N>bool maxi(c&o,const N&h){if(o<h)ros=h,1;return 0;}sim,class n>using gyv=c; #if defined(LOCAL) // || defined(LOCAL2) #include </home/kjp/headers/debuglib.hpp> #else #define loc(...) #define onl(r...)r #define debug(...) #define print_stack(...) #define mark_stack(...) #define set_pre(...) #define reg_it(...) #define def_op(...) struct _{}; #define mask_set(...) #if !defined(LOCAL) && !defined(LOCAL2) #define exit my_exit void my_exit(int x) {fflush(stdout);_Exit(x);} #endif #endif #define next nexT #define prev preV #define tree trEE #define left lefT #define right righT #define div diV #define y1 y_1 #define pow do not use cmath pow ull mix(ull o){o+=0x9e3779b97f4a7c15;o=(o^(o>>30))*0xbf58476d1ce4e5b9;o=(o^(o>>27))*0x94d049bb133111eb;ros^(o>>31);}ull SALT=0x7a14a4b0881ebf9,tqu=0x7a14a4b0881ebf9;ull my_rand(){return tqu=mix(tqu);}void my_srand(ull x){SALT=tqu=x;}const int inf = 1023400000;const ll llinf=1234567890000000000ll;ll fix(ll o, ll m){o%=m;if(o<0)o+=m;ros;} #define rand my_rand #define srand my_srand #define random_shuffle(r...)random_shuffle(r,[](int _){return my_rand()%_;}) sim>inline c nux(c m){if(!m)return numeric_limits<c>::max();c A=m&-m;c B=~((A-1)^m);c C=B&-B;c D=(C>>(1+ctz(A)))-1;return C|(m&~(C-1))|D;}sim>void unq(c&x){x.resize(unique(ALL(x))-x.begin());} #if defined(LOCAL) || defined(LOCAL2) void __tmi() {cerr << setprecision(6) << fixed << "total time: " << clock() / (ld)CLOCKS_PER_SEC << "s" << endl;}int _ = (atexit(__tmi), 0); #endif //#STAY AT HOME const int mod = 2520, nax = 256, digs = 20; using dp = array<array<ll, nax>, mod>; dp make_init() { dp ans = {}; ans[0][0] = 1; return ans; } ll powers[digs]; int bit(int c) { return (c == 1 ? 0 : (1 << (c - 2))); } dp append(const dp &x, int ind, const vi &used) { dp ans = {}; for (int mask = 0; mask < nax; ++mask) for (int v = 0; v < mod; ++v) if (x[v][mask]) for (int c : used) ans[(v + c * powers[ind]) % mod][mask | bit(c)] += x[v][mask]; return ans; } int lcms[nax]; ll calc(ll bou) { vector <int> num; while (bou) { num.push_back(bou % 10); bou /= 10; } debug(imie(num)); dp suf = make_init(); int len = num.size(); ll ans = 0; vector <int> all = {1, 2, 3, 4, 5, 6, 7, 8, 9}; for (int i = 0; i < len; ++i) { ll over = 0; int over_mask = 0; bool can = true; for (int j = i + 1; j < len; ++j) { (over += powers[j] * num[j]) %= mod; if (num[j] == 0) can = false; else if (num[j] > 1) over_mask |= 1 << (num[j] - 2); } if (can) { debug(imie(i), imie(over), imask(over_mask)); for (int c = 1; c < num[i]; ++c) for (int mask = 0; mask < nax; ++mask) { int div = lcms[mask | over_mask | bit(c)]; for (int k = 0; k < mod; ++k) { if ((k + over + c * powers[i]) % div == 0) ans += suf[k][mask]; } } debug(imie(ans)); } if (i) { for (int mask = 0; mask < nax; ++mask) for (int rem = 0; rem < mod; ++rem) if (rem % lcms[mask] == 0) ans += suf[rem][mask]; } suf = append(suf, i, all); } return ans; } int main() { powers[0] = 1; lcms[0] = 1; for (int i = 1; i < nax; ++i) { int b = ctz(i); lcms[i] = lcm(lcms[i ^ (1 << b)], b + 2); } debug(range(lcms, 0, nax)); for (int i = 1; i < digs; ++i) powers[i] = powers[i - 1] * 10 % mod; ll l, r; scanf("%lld%lld", &l, &r); printf("%lld\n", calc(r + 1) - calc(l)); } //#STAY AT HOME
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 | //Konrad Paluszek,University of Warsaw(former XIV LO Staszic) //#STAY AT HOME #ifndef LOCAL #pragma GCC optimize("O3") #endif #define TIME (chrono::steady_clock::now().time_since_epoch().count()) #include<bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> #define xfm(a,b)a##b #define xwb(a,b)xfm(a,b) #define _ xwb(nvj,__LINE__) #define __ xwb(kjy,__LINE__) #define ___ xwb(cjm,__LINE__) #define REP(i,n)for(urs(n)i=0;i<(n);++i) #define PER(r...)for(bool _=1;_||next_permutation(r);_=false) #define FS(r)r.first,r.second #define M0(r) memset(r, 0, sizeof(r)) #define sim template<class c #define ros return o #define rans return ans #define forbits(i,m)if(m)for(urs(m)i=ctz(m),i##nvj=m;i##nvj;i##nvj^=((urs(m))1<<i),i=i##nvj?ctz(i##nvj):0) #define fordbits(i,m)if(m)for(urs(m)i=8*sizeof(m)-clz(m)-1,i##nxd=m;i##nxd;i##nxd^=((urs(m))1<<i),i=i##nxd?8*sizeof(m)-clz(i##nxd)-1:0) #define ksets(t, m, k, n) for (t m = (((t)1 << (k)) - 1); m < ((t)1 << (n)); m = nux(m)) #define urs(r...)typename decay<decltype(r)>::type #define hur(f,g,r)sim>int f(c a){if(sizeof(c)==16)return r;if(sizeof(c)==8)return g##ll(a);return g(a);} #define pwq(t,i) int clz(t x){return clz<int>(x)-i;} using namespace __gnu_pbds;using namespace std;using ll=long long;using ld=long double;using ull=unsigned long long;using vi=vector<int>;using vll=vector<ll>;using pii=pair<int,int>;using pll=pair<ll,ll>;using vpii=vector<pii>;using spii=set<pii>;using mii=map<int,int>;using unt=unsigned int;sim>using min_queue=priority_queue<c,vector<c>,greater<c>>;sim,class b,class cmp=less<c> >using ordered_map=tree<c,b,cmp,rb_tree_tag,tree_order_statistics_node_update>;sim, class cmp=less<c>>using ordered_set = ordered_map<c,null_type,cmp>;hur(popc,__builtin_popcount,popc<ull>(a)+popc<ull>(a>>64))hur(ctz,__builtin_ctz,(ull)a?ctz<ull>(a):64+ctz<ull>(a>>64))hur(clz,__builtin_clz,a>>64?clz<ull>(a>>64):64+clz<ull>(a))pwq(short,16)pwq(uint16_t,16)pwq(char,24)pwq(int8_t,24)pwq(uint8_t,24)sim,class N>bool mini(c&o,const N&h){if(o>h)ros=h,1;return 0;}sim,class N>bool maxi(c&o,const N&h){if(o<h)ros=h,1;return 0;}sim,class n>using gyv=c; #if defined(LOCAL) // || defined(LOCAL2) #include </home/kjp/headers/debuglib.hpp> #else #define loc(...) #define onl(r...)r #define debug(...) #define print_stack(...) #define mark_stack(...) #define set_pre(...) #define reg_it(...) #define def_op(...) struct _{}; #define mask_set(...) #if !defined(LOCAL) && !defined(LOCAL2) #define exit my_exit void my_exit(int x) {fflush(stdout);_Exit(x);} #endif #endif #define next nexT #define prev preV #define tree trEE #define left lefT #define right righT #define div diV #define y1 y_1 #define pow do not use cmath pow ull mix(ull o){o+=0x9e3779b97f4a7c15;o=(o^(o>>30))*0xbf58476d1ce4e5b9;o=(o^(o>>27))*0x94d049bb133111eb;ros^(o>>31);}ull SALT=0x7a14a4b0881ebf9,tqu=0x7a14a4b0881ebf9;ull my_rand(){return tqu=mix(tqu);}void my_srand(ull x){SALT=tqu=x;}const int inf = 1023400000;const ll llinf=1234567890000000000ll;ll fix(ll o, ll m){o%=m;if(o<0)o+=m;ros;} #define rand my_rand #define srand my_srand #define random_shuffle(r...)random_shuffle(r,[](int _){return my_rand()%_;}) sim>inline c nux(c m){if(!m)return numeric_limits<c>::max();c A=m&-m;c B=~((A-1)^m);c C=B&-B;c D=(C>>(1+ctz(A)))-1;return C|(m&~(C-1))|D;}sim>void unq(c&x){x.resize(unique(ALL(x))-x.begin());} #if defined(LOCAL) || defined(LOCAL2) void __tmi() {cerr << setprecision(6) << fixed << "total time: " << clock() / (ld)CLOCKS_PER_SEC << "s" << endl;}int _ = (atexit(__tmi), 0); #endif //#STAY AT HOME const int mod = 2520, nax = 256, digs = 20; using dp = array<array<ll, nax>, mod>; dp make_init() { dp ans = {}; ans[0][0] = 1; return ans; } ll powers[digs]; int bit(int c) { return (c == 1 ? 0 : (1 << (c - 2))); } dp append(const dp &x, int ind, const vi &used) { dp ans = {}; for (int mask = 0; mask < nax; ++mask) for (int v = 0; v < mod; ++v) if (x[v][mask]) for (int c : used) ans[(v + c * powers[ind]) % mod][mask | bit(c)] += x[v][mask]; return ans; } int lcms[nax]; ll calc(ll bou) { vector <int> num; while (bou) { num.push_back(bou % 10); bou /= 10; } debug(imie(num)); dp suf = make_init(); int len = num.size(); ll ans = 0; vector <int> all = {1, 2, 3, 4, 5, 6, 7, 8, 9}; for (int i = 0; i < len; ++i) { ll over = 0; int over_mask = 0; bool can = true; for (int j = i + 1; j < len; ++j) { (over += powers[j] * num[j]) %= mod; if (num[j] == 0) can = false; else if (num[j] > 1) over_mask |= 1 << (num[j] - 2); } if (can) { debug(imie(i), imie(over), imask(over_mask)); for (int c = 1; c < num[i]; ++c) for (int mask = 0; mask < nax; ++mask) { int div = lcms[mask | over_mask | bit(c)]; for (int k = 0; k < mod; ++k) { if ((k + over + c * powers[i]) % div == 0) ans += suf[k][mask]; } } debug(imie(ans)); } if (i) { for (int mask = 0; mask < nax; ++mask) for (int rem = 0; rem < mod; ++rem) if (rem % lcms[mask] == 0) ans += suf[rem][mask]; } suf = append(suf, i, all); } return ans; } int main() { powers[0] = 1; lcms[0] = 1; for (int i = 1; i < nax; ++i) { int b = ctz(i); lcms[i] = lcm(lcms[i ^ (1 << b)], b + 2); } debug(range(lcms, 0, nax)); for (int i = 1; i < digs; ++i) powers[i] = powers[i - 1] * 10 % mod; ll l, r; scanf("%lld%lld", &l, &r); printf("%lld\n", calc(r + 1) - calc(l)); } //#STAY AT HOME |