Have some idea about Remainder Theorem, not necessary to solve the problem but it is good to revise some theorem.
#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;
}