版本比较
P1054 [NOIP2005 提高组] 等价表达式 题解
正文
增加 12 行删除 12 行
10 行未改动1112131415151617181925 行未改动4546474849495051525354555657585959606162636465656667686970717172737475767778798081818283848586868788899013 行未改动10410510610710810810911011111235 行未改动1481491501511521521531541551565 行未改动1621631641651661661671681691709 行未改动1801811821831841841851861871871881891901912 行未改动10 行未改动然后考虑乘和乘方。先考虑朴素的 $O(n^2)$ 乘法。(~~通过手工模拟可得~~),代码为:
```c++for (int i = 0; i < K; i++) for (int j = 0; i + j < K; j++) for (int j = 0; i + j < K; j++) r.k[i + j] += k[i] * x.k[j];```
如果要求更高的速度,可以用 FFT 优化。但是本题数据范围比较小,所以不用。25 行未改动
class poly {public: int k[K];
poly () { memset (k, 0, sizeof k); } poly (const poly &x) { memcpy (k, x.k, sizeof k); } poly &operator = (const poly &x) { memcpy (k, x.k, sizeof k); return *this; } poly (int x, bool flag = false) { memset (k, 0, sizeof k), k[flag] = x; } poly operator + (const poly &x) { poly r; for (int i = 0; i < K; i++) r.k[i] = (k[i] + x.k[i]) % P; return r; }
poly operator - (const poly &x) { poly r; for (int i = 0; i < K; i++) r.k[i] = (k[i] + P - x.k[i]) % P; return r; }
poly operator * (const poly &x) { poly r; for (int i = 0; i < K; i++) for (int j = 0; i + j < K; j++) (r.k[i + j] += k[i] * x.k[j] % P) %= P; return r; }
poly operator ^ (int x) { poly r = *this, p = *this; --x; while (x) { if (x & 1) r = r * p; x >>= 1, p = p * p; } return r; }
bool operator == (const poly &x) { for (int i = 0; i < K; i++) if (k[i] != x.k[i]) return false; return true; }
friend ostream &operator << (ostream &out, const poly &x) { for (int i = K - 1; i >= 2; i--) if (x.k[i]) { if (x.k[i] == 1) out << "a^" << i << " + "; else if (x.k[i] == -1) out << "-a^" << i << " + ";13 行未改动
void ins (char c){ poly y, x;
if (c == '(') return; if (q.empty ()) cout << "fuck off", exit (0); y = q.top (), q.pop (); if (q.empty ()) cout << "fuck off", exit (0);35 行未改动 q.push (poly (x)); if (c == '\n') break; else continue; }
switch (c) { case 'a': q.push (poly (1, true)); break; case '(': op.push ('('), level++; break; case ')':5 行未改动 op.push (c); break; } while (space (c = getchar ()) && c != '\n'); } while (c != '\n');
while (!op.empty ()) ins (op.top ()), op.pop (); return q.top ();}
9 行未改动signed main (void){ int n; poly res;
res = expand (); n = read ();
for (int i = 1; i <= n; i++) if (expand () == res) putchar ("ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i - 1]); putchar ('\n'); return 0;}2 行未改动