1009. Product of polynomials (+)Time limit MS
Memory Limit 65536 KB
Code length limit 16000 B
Program Standard author CHEN, Yue
This is supposed to find a*b where A and B are both polynomials.
Input Specification:
Each input file contains the one test case. Each case occupies 2 lines, and all line contains the information of a polynomial:k N1 aN1 N2 aN2 ... NK ANK, where K is the number of nonzero terms in the polynomial, Ni and ANi (I=1, 2, ..., K) are the exponents and Coeffi Cients, respectively. It is given that 1 <= K <=, 0 <= NK < ... < N2 < N1 <=1000.
Output Specification:
For each test case you should output the product of A and B on one line, with the same format as the input. Notice that there must is NO extra space at the end of each line. Accurate 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
The exponent and coefficients of k non-0 items of two polynomial are given, and the number of non-0 items and the exponent and coefficients of each of the outputs are multiplied.
The coefficient result retains one decimal.
#include <bits/stdc++.h> using namespace std; #define INF 0xfffffff #define MAXN 10010 int main () {#ifdef Online_judge #else freopen ("F:/cb/read.txt", "R", stdin);
/freopen ("F:/cb/out.txt", "w", stdout);
#endif Ios::sync_with_stdio (FALSE);
Cin.tie (0);
int n;
Double a[maxn],b[maxn],ans[maxn];//Phase Multiplier Group Open Large memset (a,0,sizeof (a));
Memset (b,0,sizeof (b));
memset (ans,0,sizeof (ans));
cin>>n;
int ma=0,mb=0,cnt=0;
while (n--) {int x;
Double y;
cin>>x>>y;
Ma=max (MA,X);
A[x]=y;
} cin>>n;
while (n--) {int x;
Double y;
cin>>x>>y;
Mb=max (MB,X);
B[x]=y;
} for (int i=0, i<=ma; ++i) for (int j=0; j<=mb; ++j) ans[i+j]+= (A[i]*b[j]);
int POS=MA+MB;
for (int i=ma+mb; i>=0;-I.) if (ans[i]!=0) ++cnt,pos=min (pos,i);
cout<<cnt<< "";
for (int i=ma+mb; i>pos;-I.) if (ans[i]!=0) cout<<i<< "" <<setiosflags (ios::fixed) <<setprecision (1) <<ans[i]<
< "";
cout<<pos<< "" <<setiosflags (ios::fixed) <<setprecision (1) <<ans[pos]<<endl;}