#include <iostream> typedef unsigned long long uint64; uint64 squaresum(uint64 x) { uint64 sum = 0; while (x > 0) { sum += (x % 10) * (x % 10); x /= 10; } return sum; } uint64 maxsquaresumoflower(uint64 x) { uint64 sum = 0; while (x > 0) { sum += 81; x /= 10; } return sum; } uint64 minn(uint64 k, uint64 a) { return ((a + (k - 1)) / k) * k; } uint64 maxn(uint64 k, uint64 b) { uint64 const n = k * maxsquaresumoflower(b); return n < b ? n : b; } int main() { uint64 k, a, b; std::cin >> k >> a >> b; a = minn(k, a); b = maxn(k, b); b = maxn(k, b); b = maxn(k, b); b = maxn(k, b); uint64 count = 0; while (a <= b) { if (squaresum(a) * k == a) { count++; } a += k; } std::cout << count << std::endl; }
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 | #include <iostream> typedef unsigned long long uint64; uint64 squaresum(uint64 x) { uint64 sum = 0; while (x > 0) { sum += (x % 10) * (x % 10); x /= 10; } return sum; } uint64 maxsquaresumoflower(uint64 x) { uint64 sum = 0; while (x > 0) { sum += 81; x /= 10; } return sum; } uint64 minn(uint64 k, uint64 a) { return ((a + (k - 1)) / k) * k; } uint64 maxn(uint64 k, uint64 b) { uint64 const n = k * maxsquaresumoflower(b); return n < b ? n : b; } int main() { uint64 k, a, b; std::cin >> k >> a >> b; a = minn(k, a); b = maxn(k, b); b = maxn(k, b); b = maxn(k, b); b = maxn(k, b); uint64 count = 0; while (a <= b) { if (squaresum(a) * k == a) { count++; } a += k; } std::cout << count << std::endl; } |