poj2527——Polynomial Remains(多项式相除)
2016-03-31 14:02
507 查看
Description
Given the polynomial
a(x) = an xn + ... + a1 x + a0,
compute the remainder r(x) when a(x) is divided by xk+1.
Input
The input consists of a number of cases. The first line of each case specifies the two integers n and k (0 <= n, k <= 10000). The next n+1 integers give the coefficients of a(x), starting from a0 and ending with an. The input is terminated if n = k = -1.
Output
For each case, output the coefficients of the remainder on one line, starting from the constant coefficient r0. If the remainder is 0, print only the constant coefficient. Otherwise, print only the first d+1 coefficients for a remainder of degree d. Separate
the coefficients by a single space.
You may assume that the coefficients of the remainder can be represented by 32-bit integers.
Sample Input
Sample Output
多项式相除,由于除数为x^k+1,因此商的当前项为anx^(n-k),使得余数多项式数组中的a
=0,a[n-k]=a[n-k]-a
;然后调整a数组的长度
#include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
using namespace std;
int a[10010],n; //a保存多项式以及改变之后的多项式
int main()
{
int k,i;
scanf("%d%d",&n,&k);
while(n!=-1&&k!=-1)
{
for(i=0; i<=n; ++i)
scanf("%d",&a[i]);
for(i=n; i>=k; i--)
{
if(a[i]==0)
continue;
a[i-k]=a[i-k]-a[i];
a[i]=0;
}
int t=n;
while(a[t]==0&&t>0) //调整多项式数组长度,直到最高项的不为0
t--;
for(i=0; i<t; ++i)
if(a[i]!=0)
printf("%d ",a[i]);
printf("%d\n",a[t]);
scanf("%d%d",&n,&k);
}
return 0;
}
Given the polynomial
a(x) = an xn + ... + a1 x + a0,
compute the remainder r(x) when a(x) is divided by xk+1.
Input
The input consists of a number of cases. The first line of each case specifies the two integers n and k (0 <= n, k <= 10000). The next n+1 integers give the coefficients of a(x), starting from a0 and ending with an. The input is terminated if n = k = -1.
Output
For each case, output the coefficients of the remainder on one line, starting from the constant coefficient r0. If the remainder is 0, print only the constant coefficient. Otherwise, print only the first d+1 coefficients for a remainder of degree d. Separate
the coefficients by a single space.
You may assume that the coefficients of the remainder can be represented by 32-bit integers.
Sample Input
5 2 6 3 3 2 0 1 5 2 0 0 3 2 0 1 4 1 1 4 1 1 1 6 3 2 3 -3 4 1 0 1 1 0 5 1 0 0 7 3 5 1 2 3 4 -1 -1
Sample Output
3 2 -3 -1 -2 -1 2 -3 0 0 1 2 3 4
多项式相除,由于除数为x^k+1,因此商的当前项为anx^(n-k),使得余数多项式数组中的a
=0,a[n-k]=a[n-k]-a
;然后调整a数组的长度
#include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
using namespace std;
int a[10010],n; //a保存多项式以及改变之后的多项式
int main()
{
int k,i;
scanf("%d%d",&n,&k);
while(n!=-1&&k!=-1)
{
for(i=0; i<=n; ++i)
scanf("%d",&a[i]);
for(i=n; i>=k; i--)
{
if(a[i]==0)
continue;
a[i-k]=a[i-k]-a[i];
a[i]=0;
}
int t=n;
while(a[t]==0&&t>0) //调整多项式数组长度,直到最高项的不为0
t--;
for(i=0; i<t; ++i)
if(a[i]!=0)
printf("%d ",a[i]);
printf("%d\n",a[t]);
scanf("%d%d",&n,&k);
}
return 0;
}
相关文章推荐
- KeyChain相关参数的说明
- crossDomain、allowDomain()、allowScriptAccess三者的关系解析
- how-to-stop-non-jailbroken-pirates-theory
- 对于 NSLayoutConstraint 不执行动画的处理:
- OC copy与retain的区别:
- 最近点对问题 POJ 3714 Raid && HDOJ 1007 Quoit Design
- IntelliJ IDEA 15.0.2导入grails工程
- LeetCode之旅(16)-Climbing Stairs
- LeetCode之旅(16)-Climbing Stairs
- LeetCode之旅(16)-Climbing Stairs
- http://jingyan.baidu.com/article/d169e186b38c37436611d8fa.html
- 小觅机器人sDeno亮相Nvidia发布会 发力人工智能
- 专访小觅机器人sDeno之父:吸引百万关注的促销员
- 手机安装apk出现INSTALL_FAILED_INSUFFICIENT_STORAGE
- rails命名约定
- 【english】 see you again
- osv main启动流程分析
- ECharts曲线http://echarts.baidu.com/index.html
- leetcode之Contains Duplicate
- 利用mail.jar发送邮件(简单版)