|
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 then q(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 polynomial p(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 |
#include <iostream>
#include <cstdio>
using namespace std;
int a[10001] = {0}, b[10001] = {0};
int main()
{
int k;
while( cin >> k){
int n = 0; char temp;
while(1)
{
scanf("%d%c",&a[n++],&temp);
if(temp=='\n') break;
}
b[0] = a[0];
for( int i = 1; i < n-1; i++)
{
b[i] = b[i-1]*k + a[i];
}
int r = a[n-1]+k*b[n-2];
cout << "q(x): ";
for ( int i = 0; i < n-1; i++){
cout << b[i];
if ( i != n-2) cout << " ";
}
cout << endl;
cout << "r = " <<r << endl;
cout << endl;
}
return 0;
}
本文介绍了一种求解多项式除法问题的算法,并通过示例详细解释了如何找到商多项式及其余数的过程。输入包括整数k及多项式的系数,输出为商多项式的系数及余数。
722

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



