結果
問題 |
No.3278 Avoid Division
|
ユーザー |
![]() |
提出日時 | 2025-09-20 18:23:14 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1 ms / 2,000 ms |
コード長 | 3,959 bytes |
コンパイル時間 | 518 ms |
コンパイル使用メモリ | 52,196 KB |
実行使用メモリ | 7,720 KB |
最終ジャッジ日時 | 2025-09-20 18:23:18 |
合計ジャッジ時間 | 3,523 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 24 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:116:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 116 | scanf("%d", &n); | ~~~~~^~~~~~~~~~ main.cpp:120:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 120 | scanf("%s%d", op, as + i); | ~~~~~^~~~~~~~~~~~~~~~~~~~
ソースコード
/* -*- coding: utf-8 -*- * * 3278.cc: No.3278 Avoid Division - yukicoder */ #include<cstdio> #include<algorithm> #include<tuple> using namespace std; /* constant */ const int MAX_N = 100; const int MAX_M = 300; const int MOD = 998244353; enum { ADD, MUL, DIV }; const char sops[][8] = {"add", "mul", "div"}; /* typedef */ using tp4 = tuple<int,int,int,int>; template<const int MOD> struct MI { int v; MI(): v() {} MI(int _v): v(_v % MOD) { if (v < 0) v += MOD; } MI(long long _v): v(_v % MOD) { if (v < 0) v += MOD; } explicit operator int() const { return v; } MI operator+(const MI m) const { return MI(v + m.v); } MI operator-(const MI m) const { return MI(v + MOD - m.v); } MI operator-() const { return MI(MOD - v); } MI operator*(const MI m) const { return MI((long long)v * m.v); } MI &operator+=(const MI m) { return (*this = *this + m); } MI &operator-=(const MI m) { return (*this = *this - m); } MI &operator*=(const MI m) { return (*this = *this * m); } bool operator==(const MI m) const { return v == m.v; } bool operator!=(const MI m) const { return v != m.v; } MI pow(int n) const { // a^n % MOD MI pm = 1, a = *this; while (n > 0) { if (n & 1) pm *= a; a *= a; n >>= 1; } return pm; } MI inv() const { return pow(MOD - 2); } MI operator/(const MI m) const { return *this * m.inv(); } MI &operator/=(const MI m) { return (*this = *this / m); } }; using mi = MI<MOD>; /* global variables */ int ops[MAX_N], as[MAX_N]; mi mias[MAX_N]; tp4 prgs[MAX_M]; /* subroutines */ mi calc(int n) { mi x = 0; for (int i = 0; i < n; i++) switch (ops[i]) { case ADD: x += mias[i]; break; case MUL: x *= mias[i]; break; case DIV: x /= mias[i]; break; } return x; } void printvar(int i) { if (i < 26) putchar('a' + i); else printf("A[%d]", (i - 26) + 1); } mi execprgs(int m) { int cs[3] = {}; mi vs[26] = {}; for (int i = 0; i < m; i++) { auto [op, x, y, z] = prgs[i]; mi &vx = (x < 26) ? vs[x] : mias[x - 26]; mi &vy = (y < 26) ? vs[y] : mias[y - 26]; mi &vz = (z < 26) ? vs[z] : mias[z - 26]; switch (op) { case ADD: vx = vy + vz; break; case MUL: vx = vy * vz; break; case DIV: vx = vy / vz; break; } cs[op]++; } if (cs[ADD] > 100 || cs[MUL] > 100 || cs[DIV] > 1) { printf("Too many operations: %d %d %d\n", cs[ADD], cs[MUL], cs[DIV]); exit(0); } return vs[0]; } /* main */ int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { char op[4]; scanf("%s%d", op, as + i); ops[i] = (op[0] == '+') ? ADD : (op[0] == '*') ? MUL : DIV; mias[i] = as[i]; } // a/b+c=(a+b*c)/b // a/b*c=(a*c)/b // a/b/c=a/(b*c) int m = 0, dc = 0; for (int i = 0; i < n; i++) { int opi = ops[i], ai = as[i]; if (dc == 0) { if (opi == ADD) { prgs[m++] = {ADD, 0, 0, i + 26}; // A += as[i] } else if (opi == MUL) { if (as[i] != 1) prgs[m++] = {MUL, 0, 0, i + 26}; // A *= as[i] } else { // opi == DIV if (as[i] != 1) { prgs[m++] = {ADD, 1, 1, i + 26}; // B += as[i] dc++; } } } else { // dc > 0 if (opi == ADD) { prgs[m++] = {MUL, 2, 1, i + 26}; // C = B * as[i] prgs[m++] = {ADD, 0, 0, 2}; // A += C } else if (opi == MUL) { if (as[i] != 1) prgs[m++] = {MUL, 0, 0, i + 26}; // A *= as[i] } else { // opi == DIV if (as[i] != 1) prgs[m++] = {MUL, 1, 1, i + 26}; // B *= as[i] } } } if (dc > 0) { prgs[m++] = {DIV, 0, 0, 1}; // A /= B } /* mi cc = calc(n); mi pc = execprgs(m); printf(" cc=%d, pc=%d: ", (int)cc, (int)pc); if (cc == pc) puts("OK"); else puts("NG"); */ printf("%d\n", m); for (int i = 0; i < m; i++) { auto [op, x, y, z] = prgs[i]; printf("%s ", sops[op]); printvar(x); putchar(' '); printvar(y); putchar(' '); printvar(z); putchar('\n'); } return 0; }