P - Polynomial Remains ACM

Description

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






#include <stdio.h>
#include <string.h>
#include <algorithm>


using namespace std;
const int N = 10005;


int n, k, a[N];


int main () {
	while (scanf("%d%d", &n, &k) == 2 && n != -1 && k != -1) {
		for (int i = n; i >= 0; i--)
			scanf("%d", &a[i]);


		int t = max(n - k, -1);
		for (int i = 0; i <= t; i++)
			a[i+k] -= a[i];


		printf("%d", a[n]);
		for (int i = n - 1; i > t; i--)
			printf(" %d", a[i]);
		printf("\n");
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值