# 【11.9校内测试】【倒计时1天】【ak欢乐赛】【多项式计算模拟】

Solution

Code

#include<bits/stdc++.h>
#define mod 10007
using namespace std;

bool pd(char t) { return isdigit(t) || t == 'x'; }
int level(char t) { if(t == '+' || t == '-') return 1; if(t == '*')    return 2; return 0; }

char s[1005], q[1005];
int tp, tp1, i;

struct Node {
int v[1005];
int siz;
Node operator + (const Node &a) const {
Node c;
memset(c.v, 0, sizeof(c.v));
c.siz = max(siz, a.siz);
for(int i = 0; i <= c.siz; i ++)    c.v[i] = (v[i] + a.v[i]) % mod;
while(c.siz && c.v[c.siz] == 0)    c.siz --;
return c;
}
Node operator - (const Node &a) const {
Node c;
memset(c.v, 0, sizeof(c.v));
c.siz = max(siz, a.siz);
for(int i = 0; i <= c.siz; i ++)    c.v[i] = ((v[i] - a.v[i]) % mod + mod) % mod;
while(c.siz && c.v[c.siz] == 0)    c.siz --;
return c;
}
Node operator * (const Node &a) const {
Node c;
memset(c.v, 0, sizeof(c.v));
c.siz = siz + a.siz;
for(int i = 0; i <= siz; i ++)
for(int j = 0; j <= a.siz; j ++)
c.v[i + j] = (c.v[i + j] + v[i] * a.v[j] % mod) % mod;
while(c.siz && c.v[c.siz] == 0)    c.siz --;
return c;
}
} q1[1005], zero;

Node nd = zero;
if(s[i] == 'x') {
nd.siz = 1;
nd.v[1] = 1;
i ++;
return nd;
} else {
int now = 0;
while(isdigit(s[i])) now = now * 10 + s[i] - '0', i ++;
nd.v[0] = now;
return nd;
}
}

void cal() {
Node b = q1[tp1 --], a = q1[tp1 --];
char t = q[tp --];
if(t == '*')    q1[++tp1] = a * b;
else if(t == '+')    q1[++tp1] = a + b;
else if(t == '-')    q1[++tp1] = a - b;
}

void push_char(char t) {
if(level(t) >= 1) {
while(tp && level(t) <= level(q[tp]))    cal();
q[++tp] = t;
} else if(t == '(')    q[++tp] = t;
else {
while(q[tp] != '(')    cal();
tp --;
}
}

int main() {
freopen("simplify.in", "r", stdin);
freopen("simplify.out", "w", stdout);
scanf("%s", s + 1);    int len = strlen(s + 1);
s[0] = '(', s[++len] = ')';
while(i <= len) {
}