0

The problem is this http://www.spoj.com/problems/CMPLS/

I thought of implementing newtons divided difference and find out the polynomial. But I am not able to get the correct answer. Some basic test cases are passing but some are not.

# include <stdio.h>

int main()
{
    int i, j, k, t, m, x[100][100], s, c, n;
    long ans, mul;

    scanf ("%d", &t);

    while (t--) {
            scanf ("%d%d", &s, &c);

            n = s;

            for (i = 0; i < n; i++)
                    scanf ("%d", &x[i][0]);

            for (j = 1; j < n; j++)
                    for (i = 0; i < n; i++) {
                            x[i][j] = x[i+1][j-1] - x[i][j-1];
                    }

            k = n;

            printf("\n");

            for (i = 0; i < n; i++) {
                    for(j = 0; j < k; j++)
                            printf ("%d ", x[i][j]);
                    printf ("\n");
                    k--;
            }

            printf ("\n\n");

            for (m = 1; m <= c; m++) {
                    ans = x[0][0];

                    for ( i = 1; i < n - 1; i++) {
                            mul = x[0][i];
                           for (j = 1; j <= i; j++)
                                    mul = mul * (s + m - j);

                            ans = ans + mul;
                    }

                    printf ("%ld ", ans);
            }
            printf ("\n");
    }

    return 0;

}
Abhi80
  • 31
  • 2
  • 4
  • 2
    The idea of SPOJ and other online challenges is that you solve the problem yourself, or by researching the topic if you can't. – Weather Vane Oct 29 '15 at 14:52
  • The problem itself apart, are you sure your output is the correct format required by the question example? I notice `printf ("\n\n");` in your code, which would be unusual. There are no blank lines in the example answer. – Weather Vane Oct 29 '15 at 14:53
  • I am not outputting according to what spoj wants. I am even outputting the divided difference table. I just want to know am I on the right track ? – Abhi80 Oct 29 '15 at 14:56
  • 1
    Finish it. Get correct output for samples. Try Edge cases. Submit. Then see the verdict. Does it say you got it at first try? Try another way. That's the way I would follow. – Shakil Ahamed Oct 29 '15 at 15:48
  • 1
    Rather than "not able to get the correct answer" post the input, output and expected output. – chux - Reinstate Monica Oct 29 '15 at 16:21
  • How long is your `long` type ? My guess is that both `sizeof(int)` and `sizeof(long)` are 4... and `ans` overflows. Could you try to use `long long int ans` ? Does it change something ? – francis Oct 29 '15 at 18:18

0 Answers0