#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; typedef long long lld; typedef double lf; typedef long double llf; typedef pair<int,int> pii; typedef pair<lld,lld> pll; #define For(i,s,a) for(int i = (int)s; i < (int)a; ++i) #define rpt(s, it) for(auto it = s.begin(); it != s.end(); ++it) #define brpt(s, it) for(auto it = s.rend(); it != s.rbegin(); --it) #define sz size() #define pb push_back #define eb emplace_back #define ff first #define dd second #define mp make_pair template<typename Ta, typename Tb> ostream & operator <<(ostream & os, pair<Ta, Tb> x){ return os << x.ff << " " << x.dd; } /*#define ONLINE_JUDGE #ifdef ONLINE_JUDGE freopen("cottages.in" , "r" , stdin); freopen("cottages.out" , "w" , stdout); #endif */ lld dp[19][2520][512]; bitset<512>divs[2521]; lld dplen[19]; #define f(x) ((x) >= 2520 ? (x) % 2520 : x) #define g(x) ((x) >= 2520 ? (x) - 2520 : x) const int suplen = 10; void prep(){ lld pass[2520]; dp[0][0][0] = 0; For(i, 1, 10) dp[1][i][1 << (i - 1)] = 1; For(len, 1, suplen) For(dig, 1, 10) For(mask, 0, 512){ int msk = mask | ( 1 << (dig - 1) ); int rst = dig; For(rest, 0, 2520){ dp[len + 1][rst][msk] += dp[len][rest][mask]; rst += 10; if(rst > 2519) rst -= 2520; } } } void gendivs(){ For(mask, 0, 512){ lld lcm = 1; for(lld i = 1; i < 10; ++i) if(mask & (1ll << (i - 1))) lcm = lcm * i / __gcd(lcm, i); for(int hwnd = lcm; hwnd < 2520; hwnd += lcm){ divs[hwnd][mask] = 1; } } For(len, 1, 19) For(mask, 0, 512) For(rest, 0, 2520) dplen[len] += dp[len][rest][mask] * divs[rest][mask]; } lld slowcheck(lld x){ lld X = x; while(x){ if(x % 10ll == 0)return 0; if(X % (x % 10ll) != 0) return 0; x /= 10ll; } return 1; } lld get(lld x){ lld X = x; lld wyn = 0; lld pot = 1; lld len = 0; while(pot < x) pot *= 10ll, ++len; pot /= 10ll; lld Len = len; For(i, 1, Len) wyn += dplen[i]; lld prefmask = 0; lld prefrest = 0; bool erlor = 0; while(pot){ lld dig = x / pot; if(!dig) erlor = 1; if(!erlor) For(d, 1, dig){ For(rest, 0, 2520) For(mask, 0, 512){ if(divs[f(rest + prefrest + pot * d)][mask | (1 << (d - 1)) | prefmask]){ wyn += dp[len - 1][rest][mask]; } } } prefmask |= (1 << (dig - 1)); prefrest = f(prefrest + dig * pot); --len; x %= pot; pot /= 10; } for(lld i = X - (X % 10ll) + 1; i < X + 1; ++i) wyn += slowcheck(i); return wyn; } int32_t main(void){ prep(); gendivs(); //For(i, 1, 19) // cout<<dplen[i]<<" "<<i<<endl; /* cout<<dp[2][99][512]; For(rest, 0, 2521) For(mask, 0, 1024) if(dp[2][rest][mask]) cout<<2<<" "<<rest<<" "<<mask<<" "<<dp[2][rest][mask]<<endl; */ lld a, b; scanf("%lld%lld", &a, &b); printf("%lld", get(b) - get(a - 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 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 | #include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; typedef long long lld; typedef double lf; typedef long double llf; typedef pair<int,int> pii; typedef pair<lld,lld> pll; #define For(i,s,a) for(int i = (int)s; i < (int)a; ++i) #define rpt(s, it) for(auto it = s.begin(); it != s.end(); ++it) #define brpt(s, it) for(auto it = s.rend(); it != s.rbegin(); --it) #define sz size() #define pb push_back #define eb emplace_back #define ff first #define dd second #define mp make_pair template<typename Ta, typename Tb> ostream & operator <<(ostream & os, pair<Ta, Tb> x){ return os << x.ff << " " << x.dd; } /*#define ONLINE_JUDGE #ifdef ONLINE_JUDGE freopen("cottages.in" , "r" , stdin); freopen("cottages.out" , "w" , stdout); #endif */ lld dp[19][2520][512]; bitset<512>divs[2521]; lld dplen[19]; #define f(x) ((x) >= 2520 ? (x) % 2520 : x) #define g(x) ((x) >= 2520 ? (x) - 2520 : x) const int suplen = 10; void prep(){ lld pass[2520]; dp[0][0][0] = 0; For(i, 1, 10) dp[1][i][1 << (i - 1)] = 1; For(len, 1, suplen) For(dig, 1, 10) For(mask, 0, 512){ int msk = mask | ( 1 << (dig - 1) ); int rst = dig; For(rest, 0, 2520){ dp[len + 1][rst][msk] += dp[len][rest][mask]; rst += 10; if(rst > 2519) rst -= 2520; } } } void gendivs(){ For(mask, 0, 512){ lld lcm = 1; for(lld i = 1; i < 10; ++i) if(mask & (1ll << (i - 1))) lcm = lcm * i / __gcd(lcm, i); for(int hwnd = lcm; hwnd < 2520; hwnd += lcm){ divs[hwnd][mask] = 1; } } For(len, 1, 19) For(mask, 0, 512) For(rest, 0, 2520) dplen[len] += dp[len][rest][mask] * divs[rest][mask]; } lld slowcheck(lld x){ lld X = x; while(x){ if(x % 10ll == 0)return 0; if(X % (x % 10ll) != 0) return 0; x /= 10ll; } return 1; } lld get(lld x){ lld X = x; lld wyn = 0; lld pot = 1; lld len = 0; while(pot < x) pot *= 10ll, ++len; pot /= 10ll; lld Len = len; For(i, 1, Len) wyn += dplen[i]; lld prefmask = 0; lld prefrest = 0; bool erlor = 0; while(pot){ lld dig = x / pot; if(!dig) erlor = 1; if(!erlor) For(d, 1, dig){ For(rest, 0, 2520) For(mask, 0, 512){ if(divs[f(rest + prefrest + pot * d)][mask | (1 << (d - 1)) | prefmask]){ wyn += dp[len - 1][rest][mask]; } } } prefmask |= (1 << (dig - 1)); prefrest = f(prefrest + dig * pot); --len; x %= pot; pot /= 10; } for(lld i = X - (X % 10ll) + 1; i < X + 1; ++i) wyn += slowcheck(i); return wyn; } int32_t main(void){ prep(); gendivs(); //For(i, 1, 19) // cout<<dplen[i]<<" "<<i<<endl; /* cout<<dp[2][99][512]; For(rest, 0, 2521) For(mask, 0, 1024) if(dp[2][rest][mask]) cout<<2<<" "<<rest<<" "<<mask<<" "<<dp[2][rest][mask]<<endl; */ lld a, b; scanf("%lld%lld", &a, &b); printf("%lld", get(b) - get(a - 1)); } |