結果

問題 No.3501 Digit Products 2
コンテスト
ユーザー Qiu Tian
提出日時 2026-04-18 15:28:36
言語 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  
実行時間 -
コード長 2,585 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,011 ms
コンパイル使用メモリ 338,816 KB
実行使用メモリ 30,320 KB
平均クエリ数 5.41
最終ジャッジ日時 2026-04-18 15:28:59
合計ジャッジ時間 17,149 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 38 WA * 34
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

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

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

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

    int N;
    cin >> N;

    vector<int> d(N, -1);

    int i = 0;

    // Process in groups of 3
    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);

        vector<array<int,3>> cand;

        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) {
                        cand.push_back({A, B, C});
                    }
                }
            }
        }

        // Must be uniquely determined
        if (cand.size() != 1) {
            cout << "! -1\n";
            cout.flush();
            return 0;
        }

        d[i]   = cand[0][0];
        d[i+1] = cand[0][1];
        d[i+2] = cand[0][2];

        i += 3;
    }

    // Handle remaining 1 digit
    if (i < N) {
        if (i == 0) {
            // cannot determine single digit alone
            cout << "! -1\n";
            cout.flush();
            return 0;
        }

        long long p = ask(i-1, i);

        if (d[i-1] == 0) {
            // ambiguous (0 * anything = 0)
            cout << "! -1\n";
            cout.flush();
            return 0;
        }

        if (p % d[i-1] != 0) {
            cout << "! -1\n";
            cout.flush();
            return 0;
        }

        d[i] = p / d[i-1];

        if (d[i] < 0 || d[i] > 9) {
            cout << "! -1\n";
            cout.flush();
            return 0;
        }
    }

    // Handle remaining 2nd digit (if exists)
    if (i + 1 < N) {
        long long p = ask(i, i+1);

        if (d[i] == 0) {
            cout << "! -1\n";
            cout.flush();
            return 0;
        }

        if (p % d[i] != 0) {
            cout << "! -1\n";
            cout.flush();
            return 0;
        }

        d[i+1] = p / d[i];

        if (d[i+1] < 0 || d[i+1] > 9) {
            cout << "! -1\n";
            cout.flush();
            return 0;
        }
    }

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

    cout << "! " << X << '\n';
    cout.flush();

    return 0;
}
0