結果

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

ソースコード

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<vector<long long>> p(N, vector<long long>(N, -1));

    // Query all pairs involving 0 and 1 (enough for reconstruction)
    for (int i = 1; i < N; i++) {
        p[0][i] = p[i][0] = ask(0, i);
    }
    if (N >= 2) {
        for (int i = 2; i < N; i++) {
            p[1][i] = p[i][1] = ask(1, i);
        }
    }

    vector<int> d(N);

    bool found = false;
    vector<int> answer;

    for (int d0 = 0; d0 <= 9; d0++) {
        for (int d1 = 0; d1 <= 9; d1++) {

            if (d0 * d1 != p[0][1]) continue;

            vector<int> cur(N, -1);
            cur[0] = d0;
            cur[1] = d1;

            bool ok = true;

            // propagate using best available constraint
            for (int i = 2; i < N; i++) {
                if (d0 != 0 && p[0][i] % d0 == 0) {
                    cur[i] = p[0][i] / d0;
                } else if (d1 != 0 && p[1][i] % d1 == 0) {
                    cur[i] = p[1][i] / d1;
                } else {
                    ok = false;
                    break;
                }

                if (cur[i] < 0 || cur[i] > 9) {
                    ok = false;
                    break;
                }
            }

            if (!ok) continue;

            // full verification (critical for WA fix)
            for (int i = 0; i < N && ok; i++) {
                for (int j = i + 1; j < N; j++) {
                    if (cur[i] * cur[j] != p[i][j]) {
                        ok = false;
                        break;
                    }
                }
            }

            if (ok) {
                if (found) {
                    cout << "! -1\n";
                    cout.flush();
                    return 0;
                }
                found = true;
                answer = cur;
            }
        }
    }

    if (!found) {
        cout << "! -1\n";
        cout.flush();
        return 0;
    }

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

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

    return 0;
}
0