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[1005] = {0}; double b[1005] = {0}; double c[maxnum] = {0}; int main(){ int n;cin >> n; int t; for(int i=0;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=0;i < m;i++){ int x;double y; cin >> x >> y; b[x] = y; if(!i) start = x; } for(int i=0;i < t+5;i++){ for(int j=0;j < start+5;j++){ c[i+j] += a[i]*b[j]; } } int cnt = 0; for(int i=0;i < maxnum;i++)if(c[i])cnt++; cout << cnt; for(int i=maxnum-1;i>=0;i--){ if(c[i]) printf(" %d %.1lf",i,c[i]); } return 0; } 空间开的有点小炸,做了一下优化就好了,再一次理解错了提议,K<10,不一定就项系数<10,还好这题没出毒瘤卡边界数据,在超过1e5的地方直接放弃了。。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |