UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#212495#3839. 小t爱算数(mul)jaylan1005ms1308kbC++111.7kb2024-10-19 10:14:192024-10-19 12:32:33

answer

#include <bits/stdc++.h>

using namespace std;
const int N = 2010;

int x, flag = 1, num, n1, n2, maxx1, maxx2, a1[N], b1[N], a2[N], b2[N], ans[N];
string s1, s2;

int main() {
	getline(cin, s1);
	getline(cin, s2);
	s1.erase(s1.size() - 2, 2);
	s2.erase(s2.size() - 2, 2);
	for (int i = 0; i < s1.size(); i++) {
		if (s1[i] >= '0' && s1[i] <= '9') num = num * 10 + s1[i] - '0';
		else if (s1[i] == '-') flag = -1;
		else if (s1[i] == ' ') {
			if (++n1 % 2 == 1) a1[(n1 + 1) / 2] = flag * num;
			else b1[n1 / 2] = flag * num;
			num = 0;
			flag = 1;
		}
	}
	n1 /= 2;
	num = 0;
	flag = 1;
	for (int i = 1; i <= n1; i++) maxx1 = max(maxx1, a1[i]);
	for (int i = 0; i < s2.size(); i++) {
		if (s2[i] >= '0' && s2[i] <= '9') num = num * 10 + s2[i] - '0';
		else if (s2[i] == '-') flag = -1;
		else if (s2[i] == ' ') {
			if (++n2 % 2 == 1) a2[(n2 + 1) / 2] = flag * num;
			else b2[n2 / 2] = flag * num;
			num = 0;
			flag = 1; 
		}
	}
	n2 /= 2;
	for (int i = 1; i <= n2; i++) maxx2 = max(maxx2, a2[i]);
	for (int i = 1; i <= n1; i++) {
		for (int j = 1; j <= n2; j++) {
			ans[a1[i] + a2[j]] += b1[i] * b2[j];
		}
	}
	for (int i = maxx1 + maxx2; i >= 0; i--) {
		if (ans[i] == 0) continue;
		if (i == maxx1 + maxx2) {
			if (i == 0) cout << ans[i] << endl;
			else if (ans[i] == 1) cout << "x^" << i;
			else if (ans[i] == -1) cout << "-x^" << i;
			else cout << ans[i] << "x^" << i;
		} else if (i == 0) {
			if (ans[i] > 0) cout << '+' << ans[i] << endl;
			else cout << ans[i] << endl;
		} else {
			if (ans[i] == 1) cout << "+x^" << i;
			else if (ans[i] == -1) cout << "-x^" << i;
			else {
				if (ans[i] > 0) cout << '+' << ans[i] << "x^" << i;
				else cout << ans[i] << "x^" << i;
			}
		}
	}
	return 0;
}

Details

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 0ms
memory: 1248kb

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:

63x^20-27x^19-63x^18+33x^17-40x^16+71x^15-75x^14+172x^13+88x^12-145x^11-16x^10-11x^9-7x^8+5x^7+15x^6...

result:

ok single line: '63x^20-27x^19-63x^18+33x^17-40...146x^5+20x^4-43x^3+6x^2-21x^1-9'

Test #2:

score: 10
Accepted
time: 0ms
memory: 1244kb

input:

4 3 2 -2 -1
4 3 2 2 -1

output:

9x^8-4x^4

result:

ok single line: '9x^8-4x^4'

Test #3:

score: 10
Accepted
time: 1ms
memory: 1248kb

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:

-126x^199+1958x^198-5539x^197-8128x^196-11858x^195-8722x^194+5062x^193-10855x^192+2208x^191-1348x^19...

result:

ok single line: '-126x^199+1958x^198-5539x^197-...^4-8639x^3+3990x^2+2463x^1-2156'

Test #4:

score: 10
Accepted
time: 0ms
memory: 1308kb

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:

-736791x^1999-60050x^1998+390877x^1997-91166x^1996+209275x^1995+495327x^1994+131675x^1993-2564166x^1...

result:

ok single line: '-736791x^1999-60050x^1998+3908...9x^3-646021x^2+537451x^1-473709'

Test #5:

score: 10
Accepted
time: 0ms
memory: 1308kb

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:

-736791x^1999-60050x^1998+390877x^1997-91166x^1996+209275x^1995+495327x^1994+131675x^1993-2564166x^1...

result:

ok single line: '-736791x^1999-60050x^1998+3908...9x^3-646021x^2+537451x^1-473709'

Test #6:

score: 10
Accepted
time: 0ms
memory: 1304kb

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:

-736791x^2000-60050x^1999+390877x^1998-91166x^1997+209275x^1996+495327x^1995+131675x^1994-2564166x^1...

result:

ok single line: '-736791x^2000-60050x^1999+3908...7x^3+221500x^2-144211x^1-256519'

Test #7:

score: 10
Accepted
time: 0ms
memory: 1248kb

input:

3 1 2 -1 0 1 -1
1 1 -1

output:

x^4-x^3+x^1

result:

ok single line: 'x^4-x^3+x^1'

Test #8:

score: 10
Accepted
time: 2ms
memory: 1308kb

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:

473709x^1998-779642x^1997-605960x^1996+71549x^1995+450635x^1994+467555x^1993+206523x^1992+711044x^19...

result:

ok single line: '473709x^1998-779642x^1997-6059...26x^3-912464x^2+110065x^1+91234'

Test #9:

score: 10
Accepted
time: 2ms
memory: 1304kb

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:

608478x^1999-492227x^1998+14839x^1997-376616x^1996+211161x^1995+576247x^1994-740738x^1993+457833x^19...

result:

ok single line: '608478x^1999-492227x^1998+1483...9x^3-605960x^2-779642x^1+473709'

Test #10:

score: 10
Accepted
time: 0ms
memory: 1308kb

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:

-256519x^2000-144211x^1999+221500x^1998-697587x^1997+856787x^1996-713790x^1995-346511x^1994+35098x^1...

result:

ok single line: '-256519x^2000-144211x^1999+221...66x^3+390877x^2-60050x^1-736791'