// Example program #include <iostream> int squares[10] = {0,1,4,9,16,25,36,49,64,81}; long long magic(long long n) { long long sum = 0; while (n > 0) { long long unity = n % 10; n = n / 10; sum += squares[unity]; } return sum; } bool match(long long k, long long n) { if (n % k == 0) { return magic(n) == n/k; } return false; } int main() { long long k, a, b; std::cin >> k >> a >> b; long long count = 0; long long remainder = a % k; if (remainder > 0) { a += k - remainder; } for (long long i = a; i <= b; i += k) { if (match(k, i)) { ++count; } } std::cout << count << std::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 | // Example program #include <iostream> int squares[10] = {0,1,4,9,16,25,36,49,64,81}; long long magic(long long n) { long long sum = 0; while (n > 0) { long long unity = n % 10; n = n / 10; sum += squares[unity]; } return sum; } bool match(long long k, long long n) { if (n % k == 0) { return magic(n) == n/k; } return false; } int main() { long long k, a, b; std::cin >> k >> a >> b; long long count = 0; long long remainder = a % k; if (remainder > 0) { a += k - remainder; } for (long long i = a; i <= b; i += k) { if (match(k, i)) { ++count; } } std::cout << count << std::endl; return 0; } |