#include <iostream> #include <vector> long long f(long long n) { long long s = 0; while (n != 0) { s += (n%10LL)*(n%10LL); n /= 10; } return s; } long long numOfDigits(long long n) { long long s = 0; while (n != 0) { s += 1; n /= 10; } return (s == 0 ? 1 : s); } int main(void) { long long k, a, b, x, n, maxX; std::vector<long long> solutions; std::cin >> k >> a >> b; x = ( a % k > 0 ? a / k + 1 : a / k ); n = x * k; maxX = numOfDigits(b) * 9*9; for(; x <= b/k; x++, n += k) { if( f(n) == x ) { solutions.push_back(n); } if ( x > maxX ) break; } std::cout << solutions.size(); 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 | #include <iostream> #include <vector> long long f(long long n) { long long s = 0; while (n != 0) { s += (n%10LL)*(n%10LL); n /= 10; } return s; } long long numOfDigits(long long n) { long long s = 0; while (n != 0) { s += 1; n /= 10; } return (s == 0 ? 1 : s); } int main(void) { long long k, a, b, x, n, maxX; std::vector<long long> solutions; std::cin >> k >> a >> b; x = ( a % k > 0 ? a / k + 1 : a / k ); n = x * k; maxX = numOfDigits(b) * 9*9; for(; x <= b/k; x++, n += k) { if( f(n) == x ) { solutions.push_back(n); } if ( x > maxX ) break; } std::cout << solutions.size(); return 0; } |