POJ-2527 Polynomial Remains-多项式相除

Polynomial Remains
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1240   Accepted: 702

Description

POJ-2527 Polynomial Remains-多项式相除_第1张图片Given the polynomial
a(x) = a n x n + ... + a 1 x + a 0,
compute the remainder r(x) when a(x) is divided by x k+1.

Input

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.

Output

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

Source

Alberta Collegiate Programming Contest 2003.10.18
#include <iostream>
#include <cstdio>
using namespace std;
const int maxn = 10010;
int main()
{
    int n,k;
    int val[maxn];
    while(scanf("%d%d",&n,&k)!=EOF,n!=-1 || k !=-1)
    {
        int i;
        for(i = 0 ; i <= n ; ++i)
        {
            scanf("%d",&val[i]);
        }
        //进行除法运算
        for(i = n ; i >= k ; --i)
        {
            if(val[i] == 0)
            {
                continue;
            }

            val[i-k] = val[i-k] - val[i];
            val[i] = 0;
        }
        //调整数组长度,即高位的0不用输出
        int t = n;
        while(val[t] == 0 && t > 0)
        {
            --t;
        }
        for(i = 0 ; i < t ; ++i)
        {
            printf("%d ",val[i]);
        }
        printf("%d\n",val[t]);
    }
    return 0;
}

因为输入输出数据太多,所以用cin、cout会超时。。

你可能感兴趣的:(C++,poj)