【数论】UVa 10586 - Polynomial Remains

Problem F: Polynomial Remains

Given the polynomial

      a(x) = an xn + ... + a1 x + a0,

compute the remainder r(x) when a(x) is divided by xk+1.

The input consists of a number of cases. The first line of each case specifies the two integers n and k (0 ≤ n, k ≤ 10000). The next n+1 integers give the coefficients of a(x), starting from a0 and ending with an. The input is terminated if n = k = -1.

For each case, output the coefficients of the remainder on one line, starting from the constant coefficient r0. If the remainder is 0, print only the constant coefficient. Otherwise, print only the first d+1 coefficients for a remainder of degree d. Separate the coefficients by a single space.

You may assume that the coefficients of the remainder can be represented by 32-bit integers.

Sample Input

5 2

6 3 3 2 0 1

5 2

0 0 3 2 0 1

4 1

1 4 1 1 1

6 3

2 3 -3 4 1 0 1

1 0

5 1

0 0

7

3 5

1 2 3 4

-1 -1

Sample Output

3 2

-3 -1

-2

-1 2 -3

0

0

1 2 3 4

题意已标黄,即求出一元n次多项式除以x^k+1后的余数,若余数为0,则输出0;否则,从0次幂系数r0开始输出最后一个不为0的系数。
按照除法的模拟过程模拟即可。简单说一下模拟过程:

比如
5 2

6 3 3 2 0 1
多项式除以x^k+1后,x大于等于k的幂的系数均变为0。比如若使x^5系数变为0,则需要(x^2+1)*(6*x^3) = 6*x^5 + 6*x^3;这样就可以将原多项式的5次幂项减掉,顺带着,3次幂项系数会减6;由此规律,每抵消掉一个x次幂,其x-2次幂的系数会相应减少x次幂的系数。这样一直到使x的k次幂系数为0就好了。这样代码就很好写了。
 1 #include<iostream>

 2 #include<cstdio>

 3 #include<cstdlib>

 4 #include<cstring>

 5 #include<cmath>

 6 using namespace std;

 7 const int N = 10005;

 8 int n, k, c[N], i;

 9 int main() {

10     while (~scanf("%d%d", &n, &k) && n != -1 || k != -1)

11     {

12         for (i = 0; i <= n; i++)

13             scanf("%d", &c[i]);

14         for (i = n; i >= k; i--)

15         {

16             c[i - k] -= c[i];

17             c[i] = 0;

18         }

19         i = n;

20         while (c[i] == 0 && i >= 0) i--;

21         if (i == -1) printf("0\n");

22         else {

23             printf("%d", c[0]);

24             for (int j = 1; j <= i; j++)

25                 printf(" %d", c[j]);

26             printf("\n");

27         }

28     }

29     return 0;

30 }



你可能感兴趣的:(main)