|
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 of p(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 then q(x) = x2 - 4x + 3 and r = 1. Again if p(x) = x3 - 7x2+ 15x - 9 and k = 3 thenq(x) = x2 - 4x + 3 and r = 0.
In this problem you have to find the quotient polynomial q(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 for k. 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 |
q(x): 1 -4 3 |
题意:
输入k,再输入一行整数 (an, an-1, … a0)
叫你根据
![]()
If k is any integer then we can write:
![]()
计算出q(x)的系数。
这题是个多项式求解的问题,只要找到规律就很简单了。
a4x4+a3x3+a2x2+a1x1+a0=x(b3x3+b2x2+b1x1+b0)-k*(b3x3+b2x2+b1x1+b0)+r
a4x4+a3x3+a2x2+a1x1+a0=b3x4+(b2-kb3)x3+(b1-kb2)x2+(b0-kb1)x+r-kbo;
1 2 3 4 5
a4 a3 a2 a1 a0
a[n-i];
an=bn-1;
a0=r-kb0;
a1=b0-kb1;
a2=b1-kb2;
a3=b2-kb3
a4=b3
bn-1=an+kbn
由于读入是倒序的所以下标转化下即可
#include<stdio.h>
#include<math.h>
const int N = 10010;
int a[N],b[N];
int main() {
int k;
int r;
char ch;
while(scanf("%d",&k) != EOF) {
int count = 0;
do{
scanf("%d",&a[count]);
count++;
}while((ch = getchar())!='\n');
b[1] = a[0];
for(int i=1; i < count;i++) {
b[i+1] = a[i] + k*b[i];
}
r = a[count-1] + k*b[count-1];
printf("q(x): ");
for(int i=1;i<count-1;i++)
printf("%d ",b[i]);
printf("%d\n",b[count-1]);
printf("r = %d\n\n",r);
}
return 0;
}
本文介绍了一种多项式除法算法,通过输入多项式的系数和除数,计算并输出商多项式的系数及余数。适用于数学、计算机科学领域的多项式运算。
338

被折叠的 条评论
为什么被折叠?



