uva 10719 - Quotient Polynomial

来源:互联网 发布:讲纪律 守底线 知敬畏 编辑:程序博客网 时间:2024/05/17 03:42

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 andk = 3 then q(x) = x2 - 4x + 3 and r = 1. Again ifp(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


Problem setter: Mohammed Shamsul Alam
Special thanks to Tanveer Ahsan

读入的时候纠结了下,当字符串读入再取出数字。借鉴了下别人的读入方法,主要是当初做字符串读入的问题以‘\N'作为结束标志可能会runtime error,不敢滥用......

剩下的就是多项式相等


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>
void main()
{int p[10001],q[10001],i,j,k,n;
 char ch;
 while (scanf("%d",&k)!=EOF)
 {n=1;
  while (scanf("%d%c",&p[n],&ch))
  {if (ch=='\n') break; ++n;}
  q[n-1]=p[1];
  for (i=n-2;i>=0;i--)
  q[i]=k*q[i+1]+p[n-i];
  printf("q(x):");
  for (i=n-1;i>=1;i--)
   printf(" %d",q[i]);  printf("\n");
  printf("r = %d\n",q[0]); printf("\n");
 }
}

原创粉丝点击