您的位置:首页 > 其它

UVA10719 Quotient Polynomial

2014-07-16 15:24 387 查看
Problem B
Quotient Polynomial
Time Limit
2 Seconds
A polynomial of degree n can be expressed as



If k is any integer then we can write:



Here q(x) is called the quotient polynomial ofp(x) of degree
(n-1) and r is any integer which is called the remainder.
For example, if p(x) = x3-
7x2+ 15x - 8 and k = 3 thenq(x) =
x2 - 4x + 3 and r = 1. Again if
p(x) = x3 - 7x2+15x - 9 and
k = 3 then q(x) = x2- 4x + 3 and r = 0.
In this problem you have to find the quotient polynomialq(x) and the remainder
r. All the input and output data will fit in 32-bit signed integer.
Input

Your program should accept an even number of lines of text. Each pair of line will represent one test case. The first line will contain an integer value fork. The second line will contain a list of integers (an,
an-1, …a0
), which represent the set of co-efficient of a polynomialp(x). Here
1 ≤ n ≤ 10000. Input is terminated by <EOF>.
Output

For each pair of lines, your program should print exactly two lines. The first line should contain the coefficients of the quotient polynomial. Print the reminder in second line. There is a blank space before and after the ‘=’ sign. Print a
blank line after the output of each test case. For exact format, follow the given sample.
Sample Input
Output for Sample Input
3

1 -7 15 -8

3

1 -7 15 -9

q(x): 1 -4 3

r = 1

q(x): 1 -4 3

r = 0

题目的意思就是给你你个方程p(x)的系数,还有k,求 根据题目中给出的变换方式,新方程q(x)的系数 和r。。

首先可以看出,q(x)系数比p(x)少一个,并且知道同一个次方的x对应的系数要是一样的。就容易知道 q(x)的第一个系数与p(x)的第一个相同。

因为最高次幂只有乘那一个。。而之后的每一个q(x)的系数都是前一个q(x)的系数乘以 k 加上 p(x)对应的这个位置的系数。。

AC代码:

#include<stdio.h>
#include<string.h>
const int N = 10010;

int main () {

int k;
int t;
int n[N];
int n2[N];
char ch;
while (scanf("%d",&k) != EOF) {
getchar();
t = 1;
scanf("%d",&n[0]);
ch = getchar();
while(1) {
if (ch != '\n')
scanf("%d",&n[t++]);
else
break;
ch = getchar();
}
if (ch == '\n') {
if (k == 0) {

printf("q(x):");
for (int i = 0 ;i < t - 1 ;i++)
printf(" %d",n[i]);
printf("\n");
printf("r = %d\n\n",n[t-1]);
continue;
}
n2[0] = n[0];
for (int i = 1; i < t; i++)
n2[i] = n2[i - 1] * k +n[i];
printf("q(x): %d",n2[0]);
for (int i = 1; i < t - 1 ;i++)
printf(" %d",n2[i]);
printf("\n");
printf("r = %d\n\n",n2[t - 1]);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: