1013: Polynomial Multiplication

 1013: Polynomial Multiplication


StatusIn/OutTIME LimitMEMORY LimitSubmit TimesSolved UsersJUDGE TYPE
stdin/stdout3s8192K3900652Standard

A polynomial is a sum of terms, where each term is a coefficient multiplied by the variable x raised to some nonnegative integer power. The largest such power is referred to as the degree of the polynomial. For example: x^3+x^2+x+1, x^3+x, x^2, and 1 are all polynomials.

Input


A positive integer denoting the number of cases.
(The degree of polynomial 1) followed by the value of each coefficient for polynomial 1.
(The degree of polynomial 2) followed by the value of each coefficient for polynomial 2.
No polynomial will have degree greater than five. All coefficients will be greater than or equal to zero and less than 65536. Each polynomial will be on a separate line. A blank line will separate each case.

Output


The product of polynomial 1 and polynomial 2. The caret symbol (^) should be used to denote exponentiation. That is, x^2 means x*x. But, instead of x^1, just use x, and instead of x^0, use nothing. Terms where the coefficient is 0 must be omitted.

Example


In this example we multiply the following two sets of polynomials:
x^4+2x^3+x^2+5x and x^3+2x^2+1
x^3+x+1 and 2x^3+x+2

Input

2
4 1 2 1 5 0
3 1 2 0 1

3 1 0 1 1
3 2 0 1 2

Output

x^7 + 4x^6 + 5x^5 + 8x^4 + 12x^3 + x^2 + 5x
2x^6 + 3x^4 + 4x^3 + x^2 + 3x + 2
#include<stdio.h>
void reverse(int *p,int n)
{
 int i,j,t;
 for(i=0,j=n;i<j;i++,j--)
 {
  t=*(p+i);
  *(p+i)=*(p+j);
  *(p+j)=t;
 }
}
int main()
{
 int n,t,a,b,i,j,s;
 scanf("%d",&n);
 for(t=0;t<n;t++)
 {
  int f[30]={0},g[30]={0},z[30]={0};
  
  scanf("%d",&a);
  for(i=0;i<=a;i++)
   scanf("%d",f+i);
  reverse(f,a);
  scanf("%d",&b);
  for(j=0;j<=b;j++)
   scanf("%d",g+j);
  reverse(g,b);
  for(i=0;i<=a;i++)
   for(j=0;j<=b;j++)
    z[i+j]+=f[i]*g[j];
  s=a+b;
 int al = 0;
 while(s >= 0)
 {
  if(z[s] != 0)
  {
   if(al == 0)
   {
    al = 1;
   }
   else
    printf(" + ");
   if(z[s]!=1 || s==0)
    printf("%d",z[s]);
   if(s != 0)
   {
    printf("x");
    if(s != 1)
     printf("^%d", s);
   }
  }
  --s;
 }
 if(al == 0) printf("0");
 printf("/n");
 }
 return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值