UVa 10719 - Quotient Polynomial

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) = x- 7x2+ 15x - 8 and k = 3 then q(x) = x- 4x + 3 and r = 1. Again if p(x) = x- 7x2+ 15x - 9 and k = 3 then q(x) = x- 4x + 3 and r = 0.

In this problem you have to find the quotient polynomial q(x) and the remainder rAll 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
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
这题目就是个纯数学的题目,只是注意一下输入就可以了,在输入an ..... a0 的时候用字符串代替,否则就出线runtime error
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
using namespace std;
int a[10010],b[10010];
string s1;
int main()
{
    int i,j,n,m,s,t,key;
    int k,r,l;
    while(scanf("%d%*c",&k)!=EOF)
    {
        k=k*-1;
        s=0;
        getline(cin,s1);
        l=s1.size();
        j=0;
        key=1;
        for(i=0;i<=l;i++)
        {
            if(s1[i]==' '||i==l)
            {
                a[j++]=s*key;
                key=1;
                s=0;
            }else if(s1[i]!='-')
            {
                s=s*10+s1[i]-'0';
            }else if(s1[i]=='-')
            {
                key=-1;
            }
        }
        n=j;
        for(i=0;i<=n-2;i++)
        {
            if(i==0)
            {
                b[i]=a[i];
            }else
            {
                b[i]=a[i]-k*b[i-1];
            }
        }
        r=a[i]-k*b[i-1];
        cout<<"q(x):" ;
        for(i=0;i<=n-2;i++)
        {
            cout<<" "<<b[i];
        }
        cout<<endl<<"r = "<<r<<endl;
        cout<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值