忘了打空行了,做的时候想起了高中的数学老师 TAT
题目:
Problem B
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 |
Special thanks to Tanveer Ahsan
解答:
1 #include<stdio.h> 2 int coe[10010]; 3 int res[10010]; 4 int main() 5 { 6 int k; 7 char c; 8 while(scanf("%d",&k)!=EOF) 9 { 10 int i; 11 k=-k; 12 for(i=0;;i++) 13 { 14 scanf("%d%c",&coe[i],&c); 15 if(c=='\n') 16 break; 17 } 18 int n=i,tmp,hd=coe[0]; 19 for(i=0;i<n;i++) 20 { 21 res[i]=hd; 22 hd=coe[i+1]-res[i]*k; 23 } 24 printf("q(x):"); 25 for(i=0;i<n;i++) 26 printf(" %d",res[i]); 27 printf("\n"); 28 printf("r = %d\n",hd); 29 printf("\n"); 30 } 31 return 0; 32 }