ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#212580 | #3839. 小t爱算数(mul) | eam2539 | 0 | 0ms | 0kb | C++11 | 2.0kb | 2024-10-19 11:57:17 | 2024-10-19 12:36:32 |
answer
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <sstream>
using namespace std;
map<int, int> multiplyPolynomials(const vector<pair<int, int>>& poly1, const vector<pair<int, int>>& poly2) {
map<int, int> result;
for (const auto& term1 : poly1) {
int exp1 = term1.first;
int coeff1 = term1.second;
for (const auto& term2 : poly2) {
int exp2 = term2.first;
int coeff2 = term2.second;
int newExp = exp1 + exp2;
int newCoeff = coeff1 * coeff2;
result[newExp] += newCoeff;
}
}
return result;
}
string formatPolynomial(const map<int, int>& poly) {
stringstream ss;
bool firstTerm = true;
for (auto it = poly.rbegin(); it != poly.rend(); ++it) {
int exp = it->first;
int coeff = it->second;
if (coeff == 0) {
continue;
}
if (!firstTerm&&coeff > 0 ) {
ss<<'+';
}
firstTerm = false;
if (exp == 0) {
ss << coeff;
} else if (coeff == 1) {
ss << "x^" << exp;
} else if (coeff == -1) {
ss << "-x^" << exp;
} else {
ss << coeff << "x^" << exp;
}
}
return ss.str();
}
int main() {
freopen("mul.in","r",stdin);
freopen("mul.out","w",stdout);
vector<pair<int, int>> poly1, poly2;
int exponent, coefficient;
while(true){
cin >> exponent;
if (exponent == -1&&getchar()!=' ') break;
cin >> coefficient;
poly1.emplace_back(exponent, coefficient);
}
while (true) {
cin >> exponent;
if (exponent == -1&&getchar()!=' ') break;
cin >> coefficient;
poly2.emplace_back(exponent, coefficient);
}
map<int, int> result = multiplyPolynomials(poly1, poly2);
cout << formatPolynomial(result) << endl;
return 0;
}
Details
小提示:点击横条可展开更详细的信息
Test #1:
score: 0
Dangerous Syscalls
input:
10 -7 9 3 7 4 6 -3 4 2 3 -7 2 -7 0 -3 -1 10 -9 8 9 7 -6 6 7 5 -2 3 -9 2 -9 1 7 0 3 -1
output:
result:
Test #2:
score: 0
Dangerous Syscalls
input:
4 3 2 -2 -1 4 3 2 2 -1
output:
result:
Test #3:
score: 0
Dangerous Syscalls
input:
100 -7 99 73 98 30 97 44 96 -23 95 -40 94 92 93 -87 92 -27 91 40 90 -3 89 -9 88 -60 87 99 86 -16 85 ...
output:
result:
Test #4:
score: 0
Dangerous Syscalls
input:
1000 -807 999 73 998 930 997 544 996 -923 995 -440 994 492 993 -987 992 -327 991 840 990 -303 989 -7...
output:
result:
Test #5:
score: 0
Dangerous Syscalls
input:
1000 -807 999 73 998 930 997 544 996 -923 995 -440 994 492 993 -987 992 -327 991 840 990 -303 989 -7...
output:
result:
Test #6:
score: 0
Dangerous Syscalls
input:
1000 -807 999 73 998 930 997 544 996 -923 995 -440 994 492 993 -987 992 -327 991 840 990 -303 989 -7...
output:
result:
Test #7:
score: 0
Dangerous Syscalls
input:
3 1 2 -1 0 1 -1 1 1 -1
output:
result:
Test #8:
score: 0
Dangerous Syscalls
input:
999 -807 998 73 997 930 996 544 995 -923 994 -440 993 492 992 -987 991 -327 990 840 989 -303 988 -70...
output:
result:
Test #9:
score: 0
Dangerous Syscalls
input:
0 -807 1 73 2 930 3 544 4 -923 5 -440 6 492 7 -987 8 -327 9 840 10 -303 11 -709 12 -560 13 99 14 -81...
output:
result:
Test #10:
score: 0
Dangerous Syscalls
input:
0 -807 1 73 2 930 3 544 4 -923 5 -440 6 492 7 -987 8 -327 9 840 10 -303 11 -709 12 -560 13 99 14 -81...