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 N1 a~N1~ N2 a~N2~ ... NK a~NK~, where K is the number of nonzero terms in the polynomial, Ni and a~Ni~ (i=1, 2, ..., K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10, 0 <= NK < ... < N2 < N1 <=1000.
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
题意:给定两个多项式,求多项式乘积的项数和结果。
思路:结构体数组存储。复杂度O(n*k)
代码:
#include <iostream>
#include <sstream>
#include <string>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = 2e3+10;
const ll INF = 0x3f3f3f3f;
const double eps=1e-4;
const int T=3;
struct Node {
int exp;
double coe;
} a[N],b[N];
double ans[N];
int main() {
int n,k;
scanf("%d",&n);
for(int i=0; i<n; i++) {
scanf("%d%lf",&a[i].exp,&a[i].coe);
}
scanf("%d",&k);
int exp;
double coe;
for(int i=0; i<k; i++) {
scanf("%d%lf",&exp,&coe);
for(int j=0; j<n; j++) {
ans[exp+a[j].exp]+=coe*a[j].coe;
}
}
int cnt=0;
for(int i=0; i<N; i++) {
if(ans[i])
cnt++;
}
printf("%d",cnt);
for(int i=N-1; i>=0; i--) {
if(ans[i])
printf(" %d %.1f",i,ans[i]);
}
return 0;
}