PAT 1009 Product of Polynomials

时间:2023-03-10 01:42:46
PAT 1009 Product of Polynomials
1009 Product of Polynomials (25 分)

This time, you are supposed to find A×B where A and B are two polynomials.

Input Specification:

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:

K N​1​​ a​N​1​​​​ N​2​​ a​N​2​​​​ ... N​K​​ a​N​K​​​​

where K is the number of nonzero terms in the polynomial, N​i​​ and a​N​i​​​​ (,) are the exponents and coefficients, respectively. It is given that 1, 0.

Output Specification:

For each test case you should output the product of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate up to 1 decimal place.

Sample Input:

2 1 2.4 0 3.2
2 2 1.5 1 0.5

Sample Output:

3 3 3.6 2 6.0 1 1.6
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define maxnum 100005 double a[] = {};
double b[] = {};
double c[maxnum] = {}; int main(){
int n;cin >> n;
int t;
for(int i=;i < n;i++){
int x;double y;
cin >> x >> y;
a[x] = y;
if(!i)t = x;
}
int m;cin >> m;
int start;
for(int i=;i < m;i++){
int x;double y;
cin >> x >> y;
b[x] = y;
if(!i) start = x;
} for(int i=;i < t+;i++){
for(int j=;j < start+;j++){
c[i+j] += a[i]*b[j];
}
} int cnt = ;
for(int i=;i < maxnum;i++)if(c[i])cnt++;
cout << cnt; for(int i=maxnum-;i>=;i--){
if(c[i]) printf(" %d %.1lf",i,c[i]);
} return ;
}

空间开的有点小炸,做了一下优化就好了,再一次理解错了提议,K<10,不一定就项系数<10,还好这题没出毒瘤卡边界数据,在超过1e5的地方直接放弃了。。