#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>
#include<set>
using namespace std;
/** K z tresci zadania */
uint64_t k;
uint64_t a;
uint64_t b;
int res=0;
set<uint64_t> found;
/**
* Funkcja sprawdzajaca, czy n spelnia wartunek k*f(n)=n
*/
bool check(uint64_t n) {
if(n%k!=0) return false; // musi byc podzielne przez K
uint64_t sum=0;
uint64_t v=n;
while(v!=0) {
int i=(v%10); // kolejne cyfry dziesietne
uint16_t vi;
vi=(i*i);
sum+=vi;
if(sum>b/k) return false; // za duzo
v/=10;
}
return (sum*k)==n;
}
void process(int v, int el, uint64_t s) {
if(v==10) {
s=s*k; // raz na koniec jak wiadomo, ze nie poza zakresem b (b/k)
if(s>=a && check(s)) {
if(found.insert(s).second) {
// printf("%llu\n",s);
++res;
}
}
return;
}
uint64_t r=s;
for(int i=0;i<=el;++i) {
if(r>b/k) return;
process(v+1,el-i,r);
r+=(v*v);
}
}
int main(int argc, char** argv) {
scanf("%llu %llu %llu",&k, &a, &b);
process(1,18,0);
printf("%d\n",res);
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 53 54 55 56 57 58 59 60 | #include<stdio.h> #include<stdlib.h> #include<stdint.h> #include<set> using namespace std; /** K z tresci zadania */ uint64_t k; uint64_t a; uint64_t b; int res=0; set<uint64_t> found; /** * Funkcja sprawdzajaca, czy n spelnia wartunek k*f(n)=n */ bool check(uint64_t n) { if(n%k!=0) return false; // musi byc podzielne przez K uint64_t sum=0; uint64_t v=n; while(v!=0) { int i=(v%10); // kolejne cyfry dziesietne uint16_t vi; vi=(i*i); sum+=vi; if(sum>b/k) return false; // za duzo v/=10; } return (sum*k)==n; } void process(int v, int el, uint64_t s) { if(v==10) { s=s*k; // raz na koniec jak wiadomo, ze nie poza zakresem b (b/k) if(s>=a && check(s)) { if(found.insert(s).second) { // printf("%llu\n",s); ++res; } } return; } uint64_t r=s; for(int i=0;i<=el;++i) { if(r>b/k) return; process(v+1,el-i,r); r+=(v*v); } } int main(int argc, char** argv) { scanf("%llu %llu %llu",&k, &a, &b); process(1,18,0); printf("%d\n",res); return 0; } |
English