結果

問題 No.3501 Digit Products 2
コンテスト
ユーザー Qiu Tian
提出日時 2026-04-18 15:22:10
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 1,893 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,007 ms
コンパイル使用メモリ 336,012 KB
実行使用メモリ 30,320 KB
平均クエリ数 11.19
最終ジャッジ日時 2026-04-18 15:22:21
合計ジャッジ時間 8,830 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 1
other AC * 36 WA * 36
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

long long ask(int a, int b) {
    cout << "? " << a << " " << b << endl;
    cout.flush();

    long long x;
    cin >> x;
    if (x == -1) exit(0);
    return x;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N;
    cin >> N;

    vector<int> d(N);

    int i = 0;
    while (i + 2 < N) {
        long long ab = ask(i, i+1);
        long long ac = ask(i, i+2);
        long long bc = ask(i+1, i+2);

        bool found = false;

        for (int A = 0; A <= 9; A++) {
            for (int B = 0; B <= 9; B++) {
                for (int C = 0; C <= 9; C++) {
                    if (A * B == ab &&
                        A * C == ac &&
                        B * C == bc) {

                        d[i] = A;
                        d[i+1] = B;
                        d[i+2] = C;
                        found = true;
                        goto done;
                    }
                }
            }
        }

        done:
        if (!found) {
            cout << "! -1" << endl;
            cout.flush();
            return 0;
        }

        i += 3;
    }

    // Handle remaining digits
    if (i < N) {
        if (i > 0) {
            long long p = ask(i-1, i);
            if (d[i-1] == 0) {
                // ambiguous → cannot determine
                cout << "! -1" << endl;
                cout.flush();
                return 0;
            }
            d[i] = p / d[i-1];
        }
    }

    if (i + 1 < N) {
        long long p = ask(i, i+1);
        if (d[i] == 0) {
            cout << "! -1" << endl;
            cout.flush();
            return 0;
        }
        d[i+1] = p / d[i];
    }

    // Build number
    long long X = 0, pw = 1;
    for (int i = 0; i < N; i++) {
        X += pw * d[i];
        pw *= 10;
    }

    cout << "! " << X << endl;
    cout.flush();
}
0