#include <iostream> #include <ctime> #include <chrono> using namespace std; typedef long long LL; #define BIT(x) (1<<x) #ifdef HOMEx #define DEBUG(x) cerr << x #else #define DEBUG(x) #endif // HOME bool inline isSpecial(LL number) { int tested = 0; for(LL i=number; i; i/=10) { short digit = i%10; if (!digit) return false; if (digit == 1) continue; if (!(tested & BIT(digit))) { if (number % digit != 0) return false; tested |= BIT(digit); } } return true; } LL solve_naive(LL l, LL r) { LL cnt=0; for(LL i=l;i<=r;++i) if (isSpecial(i)) { DEBUG(i << endl); ++cnt; } return cnt; } #define MULT(arr, x) (arr[x] == 0 ? 1 : arr[x] == 1 ? (x) : arr[x] == 2 ? (x)*(x) : (x)*(x)*(x)) LL inline countNWW(LL number) { int mult[] = {0, 0, 0, 0, 0, 0, 0, 0}; for(; number>0; number/=10) { int digit = number%10; switch(digit) { case 0: return -1; case 1: break; case 2: mult[2] = max(mult[2], 1); break; case 3: mult[3] = max(mult[3], 1); break; case 4: mult[2] = max(mult[2], 2); break; case 5: mult[5] = max(mult[5], 1); break; case 6: mult[2] = max(mult[2], 1); mult[3] = max(mult[3], 1); break; case 7: mult[7] = max(mult[7], 1); break; case 8: mult[2] = max(mult[2], 3); break; case 9: mult[3] = max(mult[3], 2); break; } } return MULT(mult, 2) * MULT(mult, 3) * MULT(mult, 5) * MULT(mult, 7); } #define DIVIDE_BASE 100000 LL solveRange(LL base, LL l, LL r) { DEBUG("range " << l << " -> " << r << "; " << base << endl); LL cnt=0; LL increment = countNWW(base); DEBUG("NWW of " << base << " is " << increment << endl); if (increment<0) // base contains 0 return 0; LL firstSpecial=0; for(LL i=l;i<=r; ++i) if (isSpecial(i)) { firstSpecial = i; break; } if (firstSpecial == 0) return 0; for(LL i=firstSpecial;i<=r;i += increment) if (isSpecial(i)) { DEBUG(i << endl); ++cnt; } return cnt; } LL solve(LL l, LL r) { LL ll = l / DIVIDE_BASE, rr = r / DIVIDE_BASE; if (ll == rr) // not enough for single range return solveRange(ll, l, r); LL cnt=0; cnt += solveRange(ll, l, (ll+1)*DIVIDE_BASE - 1); for(LL base = ll+1; base < rr; ++base) cnt += solveRange(base, base*DIVIDE_BASE, (base+1)*DIVIDE_BASE - 1); cnt += solveRange(rr, rr*DIVIDE_BASE, r); return cnt; } std::chrono::steady_clock::time_point gettime() { return std::chrono::steady_clock::now(); } LL timediff(std::chrono::steady_clock::time_point t) { return std::chrono::duration_cast<std::chrono::microseconds>(gettime()-t).count(); } int main() { ios_base::sync_with_stdio(0); LL l, r; cin>>l>>r; //auto t = gettime(); LL wyn = solve(l, r); cout << wyn; //cout << wyn << " -> " << timediff(t) << endl; //cout.flush(); //t = gettime(); //wyn = solve_naive(l, r); //cout << wyn << " -> " << timediff(t) << endl; // cout << solve(l,r) << endl; // cout << solve_naive(l,r) << 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 | #include <iostream> #include <ctime> #include <chrono> using namespace std; typedef long long LL; #define BIT(x) (1<<x) #ifdef HOMEx #define DEBUG(x) cerr << x #else #define DEBUG(x) #endif // HOME bool inline isSpecial(LL number) { int tested = 0; for(LL i=number; i; i/=10) { short digit = i%10; if (!digit) return false; if (digit == 1) continue; if (!(tested & BIT(digit))) { if (number % digit != 0) return false; tested |= BIT(digit); } } return true; } LL solve_naive(LL l, LL r) { LL cnt=0; for(LL i=l;i<=r;++i) if (isSpecial(i)) { DEBUG(i << endl); ++cnt; } return cnt; } #define MULT(arr, x) (arr[x] == 0 ? 1 : arr[x] == 1 ? (x) : arr[x] == 2 ? (x)*(x) : (x)*(x)*(x)) LL inline countNWW(LL number) { int mult[] = {0, 0, 0, 0, 0, 0, 0, 0}; for(; number>0; number/=10) { int digit = number%10; switch(digit) { case 0: return -1; case 1: break; case 2: mult[2] = max(mult[2], 1); break; case 3: mult[3] = max(mult[3], 1); break; case 4: mult[2] = max(mult[2], 2); break; case 5: mult[5] = max(mult[5], 1); break; case 6: mult[2] = max(mult[2], 1); mult[3] = max(mult[3], 1); break; case 7: mult[7] = max(mult[7], 1); break; case 8: mult[2] = max(mult[2], 3); break; case 9: mult[3] = max(mult[3], 2); break; } } return MULT(mult, 2) * MULT(mult, 3) * MULT(mult, 5) * MULT(mult, 7); } #define DIVIDE_BASE 100000 LL solveRange(LL base, LL l, LL r) { DEBUG("range " << l << " -> " << r << "; " << base << endl); LL cnt=0; LL increment = countNWW(base); DEBUG("NWW of " << base << " is " << increment << endl); if (increment<0) // base contains 0 return 0; LL firstSpecial=0; for(LL i=l;i<=r; ++i) if (isSpecial(i)) { firstSpecial = i; break; } if (firstSpecial == 0) return 0; for(LL i=firstSpecial;i<=r;i += increment) if (isSpecial(i)) { DEBUG(i << endl); ++cnt; } return cnt; } LL solve(LL l, LL r) { LL ll = l / DIVIDE_BASE, rr = r / DIVIDE_BASE; if (ll == rr) // not enough for single range return solveRange(ll, l, r); LL cnt=0; cnt += solveRange(ll, l, (ll+1)*DIVIDE_BASE - 1); for(LL base = ll+1; base < rr; ++base) cnt += solveRange(base, base*DIVIDE_BASE, (base+1)*DIVIDE_BASE - 1); cnt += solveRange(rr, rr*DIVIDE_BASE, r); return cnt; } std::chrono::steady_clock::time_point gettime() { return std::chrono::steady_clock::now(); } LL timediff(std::chrono::steady_clock::time_point t) { return std::chrono::duration_cast<std::chrono::microseconds>(gettime()-t).count(); } int main() { ios_base::sync_with_stdio(0); LL l, r; cin>>l>>r; //auto t = gettime(); LL wyn = solve(l, r); cout << wyn; //cout << wyn << " -> " << timediff(t) << endl; //cout.flush(); //t = gettime(); //wyn = solve_naive(l, r); //cout << wyn << " -> " << timediff(t) << endl; // cout << solve(l,r) << endl; // cout << solve_naive(l,r) << endl; return 0; } |