Have some idea about Remainder Theorem, not necessary to solve the problem but it is good to revise some theorem.
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 61 62 |
#include <iostream> #include <cstdio> #include <sstream> #include <string> #define ll long long using namespace std; int main() { int a,b,c,d,L; while(scanf("%d%d%d%d%d",&a,&b,&c,&d,&L)) { if(a==0 && b==0 && c==0 && d==0 && L==0) break; if(a==0 && b==0 && c==0) { printf("%d\n",L); continue; } else if(a==0 && b==0 && c!=0) { if(c%d==0) printf("%d\n",L+1); else printf("%d\n",0); continue; } else if(a==0 && b!=0 && c!=0) { if(b%d==0 && c%d==0) { printf("%d\n",L+1); } else if(b%d==0 && c%d!=0) printf("%d\n",0); continue; } else if(b==0 && a!=0 && c!=0) { if(a%d==0 && c%d==0) { printf("%d\n",L+1); } else if(a%d==0 && c%d!=0) printf("%d\n",0); continue; } ll res; ll cnt=0; for(int i=0;i<=L;i++) { res = a*i*i+b*i+c; if(res%d==0) cnt+=1; } printf("%lld\n",cnt); } return 0; } |