UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#212582#3842. 小t做晚餐2(cook)eam253900ms0kbC++111.9kb2024-10-19 11:58:252024-10-19 12:36:40

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() {
    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
Memory Limit Exceeded

input:

5 14 9
10 7 7 7 3

output:


result:


Test #2:

score: 0
Time Limit Exceeded

input:

8 14 4
7 10 1 7 8 6 10 12

output:


result:


Test #3:

score: 0
Time Limit Exceeded

input:

8 20 3
14 17 6 6 9 12 15 18

output:


result:


Test #4:

score: 0
Memory Limit Exceeded

input:

10 30 4
6 16 16 9 23 20 9 3 1 8

output:


result:


Test #5:

score: 0
Time Limit Exceeded

input:

10 41 23
17 7 7 1 8 15 17 16 12 3

output:


result:


Test #6:

score: 0
Time Limit Exceeded

input:

20 95400260 89673557
20487698 22301259 2829304 297290 1412988 16842716 23469759 30225551 23431967 16...

output:


result:


Test #7:

score: 0
Time Limit Exceeded

input:

20 67248590 51724831
648120 20516501 13360885 3511712 27876258 27548361 19789779 14004233 13644809 8...

output:


result:


Test #8:

score: 0
Memory Limit Exceeded

input:

20 32236123 23129506
9697610 3181601 12248516 13725824 1976763 3805852 10010680 13270519 12901003 78...

output:


result:


Test #9:

score: 0
Memory Limit Exceeded

input:

20 50597385 71307217
20562436 23376499 14957779 2472920 13994843 22843456 16389516 13712272 24008015...

output:


result:


Test #10:

score: 0
Memory Limit Exceeded

input:

20 79575244 7977295
21610095 13701521 24651488 4567616 15005155 6234858 15956463 15009333 5191205 59...

output:


result: