10719 - Quotient Polynomial

本文介绍了一种解决多项式除以单项式的算法问题,通过给定的多项式系数和单项式的值,计算出商多项式的系数及余数。文章提供了一个C++实现示例,并解释了如何处理输入数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

大意是:先给你一个k,组成一个一项式(x - k)。然后给你一个多项式,告诉你系数,让你求这个多项式除以一项式后的各个项系数。以及最后剩下的常数

题目不是很难,主要是在多项式系数的输入时有个手脚。由于没有告诉你一共有多少项,也没有告诉你格式。所以不好处理

我揣测数据是一个int型数字,一个空格,到最后在数字之后直接跟回车。然后对了。。要不然打算用字符串读入了

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<ctype.h>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
int main ()
{
    int k,n,i;
    while(cin>>k)
    {
        n=0;
        char ch;
        int a[10010];
        while(scanf("%d",&a[n]))
        {
            n++;
            ch=getchar();
            if (ch=='\n') break;
        }
        printf("q(x):");
        for (i=0; i<n-1; i++)
        {
            printf(" %d",a[i]);
            a[i+1]+=a[i]*k;
        }
        printf("\nr = %d\n",a[n-1]);
        cout<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值