You have to careful about the intermediate calcualtions. Although the problem description says that the output will fit in 32 bit integer, but the intermediate values may be larger than that. So, use Horner's rule to evaluate the derivative of polynomial . Also, careful about the input processing. I used gets() to take the input and strtok() to separate the coefficients.
Code: Select all
char buf[1000000];
int main()
{
// declare variables
while (gets(buf))
{
sscanf(buf, "%ld", &x);
px = 0;
ppx = 0;
gets(buf);
ptr = strtok(buf, " \n");
while (ptr)
{
a = atol(ptr);
ppx = ppx * x + px;
px = px * x + a;
ptr = strtok(NULL, " \n");
}
// print ppx
}
...
}