結果

問題 No.3278 Avoid Division
ユーザー nonon
提出日時 2025-09-19 22:43:01
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,738 bytes
コンパイル時間 2,804 ms
コンパイル使用メモリ 284,588 KB
実行使用メモリ 7,720 KB
最終ジャッジ日時 2025-09-19 22:43:08
合計ジャッジ時間 5,762 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using ll = long long;

bool chmin(auto &a, auto b) { return a > b ? a = b, true : false; }
bool chmax(auto &a, auto b) { return a < b ? a = b, true : false; }

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N;
    cin >> N;
    vector<string> ans;
    auto f = [&] (string s, int a, int b, int c) -> string {
        auto g = [&] (int i) -> string {
            return i < 0 ? "A[" + to_string(-i) + "]" : string(1, i);
        };
        return s + " " + g(a) + " " + g(b) + " " + g(c);
    };
    int di = -1;
    bool fn2 = false;
    for (int i = 1; i <= N; i++) {
        char op;
        int val;
        cin >> op >> val;
        if (op == '+') {
            if (di == -1) {
                ans.push_back(f("add", 'a', 'a', -i));
            } else if (!fn2) {
                ans.push_back(f("mul", 'b', -di, -i));
                ans.push_back(f("add", 'a', 'a', 'b'));
            } else {
                ans.push_back(f("mul", 'b', 'd', -i));
                ans.push_back(f("add", 'a', 'a', 'b'));
            }
        } else if (op == '*') {
            ans.push_back(f("mul", 'a', 'a', -i));
        } else {
            if (di == -1) {
                di = i;
            } else {
                if (!fn2) {
                    fn2 = true;
                    ans.push_back(f("mul", 'd', -di, -i));
                } else {
                    ans.push_back(f("mul", 'd', 'd', -i));
                }
            }
        }
    }
    if (di != -1) {
        if (!fn2) ans.push_back(f("div", 'a', 'a', -di));
        else ans.push_back(f("div", 'a', 'a', 'd'));
    }
    cout << ans.size() << '\n';
    for (auto s : ans) cout << s << '\n';
}
0