#include <iostream> using namespace std; namespace { constexpr auto max_s = 9*9*18; int sqr(int x) { return x*x; } int f(long long n) { int res = 0; while (n > 0) { res += sqr(n % 10); n /= 10; } return res; } int solve(long long k, long long a, long long b) { int res = 0; for (long long s = (a+k-1) / k; s <= max_s && s <= b/k; ++s) { if (f(k*s) == s) ++res; } return res; } } int main() { iostream::sync_with_stdio(false); cin.tie(NULL); long long k, a, b; cin >> k >> a >> b; cout << solve(k, a, b) << '\n'; 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 | #include <iostream> using namespace std; namespace { constexpr auto max_s = 9*9*18; int sqr(int x) { return x*x; } int f(long long n) { int res = 0; while (n > 0) { res += sqr(n % 10); n /= 10; } return res; } int solve(long long k, long long a, long long b) { int res = 0; for (long long s = (a+k-1) / k; s <= max_s && s <= b/k; ++s) { if (f(k*s) == s) ++res; } return res; } } int main() { iostream::sync_with_stdio(false); cin.tie(NULL); long long k, a, b; cin >> k >> a >> b; cout << solve(k, a, b) << '\n'; return 0; } |