#include <iostream> #include <set> #include <vector> #include <cmath> using namespace std; #define log if (false) cerr typedef long long ll; typedef unsigned long long ull; const ull INF = 1000000000000000000ull; int numberOfDigits(ull x) { int result = 0; while (x > 0) { result++; x /= 10; } return result; } set<ull> computeFns(ull maxPossible) { int maxPosition = numberOfDigits(maxPossible); set<ull> fns; fns.insert(0); for (int pos = 1; pos <= maxPosition; pos++) { set<int> newFns; for (int digit = 1; digit <= 9; digit++) { for (auto fn : fns) { newFns.insert(fn + digit * digit); } } fns.insert(newFns.begin(), newFns.end()); } fns.erase(0); return fns; } ull fun(ull n) { ull result = 0; while (n > 0) { result += (n % 10) * (n % 10); n /= 10; } return result; } int solve(const set<ull>& fns, ull a, ull b, ull k) { ull maxFn = b / k; ull minFn = a / k; int count = 0; for (auto fn : fns) { if (fn < minFn) { continue; } if (fn > maxFn) { break; } ull n = k * fn; if (n >= a && n <= b && fun(n) == fn) { count++; log << n << endl; } } return count; } int main() { ull a, b, k; a = 4080; b = 7905; k = 51; // a = 1; b = INF; k = INF / 200; cin >> k >> a >> b; set<ull> fns = computeFns(b); cout << solve(fns, a, b, k); 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 | #include <iostream> #include <set> #include <vector> #include <cmath> using namespace std; #define log if (false) cerr typedef long long ll; typedef unsigned long long ull; const ull INF = 1000000000000000000ull; int numberOfDigits(ull x) { int result = 0; while (x > 0) { result++; x /= 10; } return result; } set<ull> computeFns(ull maxPossible) { int maxPosition = numberOfDigits(maxPossible); set<ull> fns; fns.insert(0); for (int pos = 1; pos <= maxPosition; pos++) { set<int> newFns; for (int digit = 1; digit <= 9; digit++) { for (auto fn : fns) { newFns.insert(fn + digit * digit); } } fns.insert(newFns.begin(), newFns.end()); } fns.erase(0); return fns; } ull fun(ull n) { ull result = 0; while (n > 0) { result += (n % 10) * (n % 10); n /= 10; } return result; } int solve(const set<ull>& fns, ull a, ull b, ull k) { ull maxFn = b / k; ull minFn = a / k; int count = 0; for (auto fn : fns) { if (fn < minFn) { continue; } if (fn > maxFn) { break; } ull n = k * fn; if (n >= a && n <= b && fun(n) == fn) { count++; log << n << endl; } } return count; } int main() { ull a, b, k; a = 4080; b = 7905; k = 51; // a = 1; b = INF; k = INF / 200; cin >> k >> a >> b; set<ull> fns = computeFns(b); cout << solve(fns, a, b, k); return 0; } |