結果

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

ソースコード

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);
    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 chain (N-1 queries)
    for (int i = 1; i < N; i++) {
        p[0][i] = p[i][0] = ask(0, i);
    }

    // Extra queries to resolve ambiguity
    if (N >= 3) {
        p[1][2] = p[2][1] = ask(1, 2);
    }

    vector<vector<int>> solutions;

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

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

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

            bool ok = true;

            for (int i = 2; i < N; i++) {
                if (d0 != 0) {
                    if (p[0][i] % d0 != 0) { ok = false; break; }
                    d[i] = p[0][i] / d0;
                } else if (d1 != 0) {
                    if (p[1][i] == -1) {
                        p[1][i] = p[i][1] = ask(1, i);
                    }
                    if (p[1][i] % d1 != 0) { ok = false; break; }
                    d[i] = p[1][i] / d1;
                } else {
                    // both zero → completely ambiguous
                    ok = false;
                    break;
                }

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

            // Validate all known constraints
            for (int i = 0; i < N && ok; i++) {
                for (int j = i+1; j < N && ok; j++) {
                    if (p[i][j] != -1) {
                        if (d[i] * d[j] != p[i][j]) {
                            ok = false;
                        }
                    }
                }
            }

            if (ok) solutions.push_back(d);
        }
    }

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

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

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