#include <cstdio>
const int maxSumOfDigits = (18 + 1) * (9 * 9 + 1);
long long k, a, b;
int SumOfSquaresOfDigits(long long n) {
int result = 0;
while (n > 0) {
int last_digit = n % 10;
n /= 10;
result += last_digit * last_digit;
}
return result;
}
bool IsNumberOk(long long n) {
return a <= n and n <= b
and n % k == 0 and n / k == SumOfSquaresOfDigits(n);
}
int main() {
scanf("%lld%lld%lld", &k, &a, &b);
int result = 0;
for (int i = 1; k * i <= b and i <= maxSumOfDigits; i++) {
if (IsNumberOk(k * i)) {
result++;
}
}
printf("%d\n", result);
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 | #include <cstdio> const int maxSumOfDigits = (18 + 1) * (9 * 9 + 1); long long k, a, b; int SumOfSquaresOfDigits(long long n) { int result = 0; while (n > 0) { int last_digit = n % 10; n /= 10; result += last_digit * last_digit; } return result; } bool IsNumberOk(long long n) { return a <= n and n <= b and n % k == 0 and n / k == SumOfSquaresOfDigits(n); } int main() { scanf("%lld%lld%lld", &k, &a, &b); int result = 0; for (int i = 1; k * i <= b and i <= maxSumOfDigits; i++) { if (IsNumberOk(k * i)) { result++; } } printf("%d\n", result); return 0; } |
English