結果

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

ソースコード

diff #
raw source code

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

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

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

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

    int N;
    cin >> N;

    int r = N - 1;                 // leading digit position, guaranteed nonzero
    vector<long long> p(N, -1);    // p[i] = d[i] * d[r] for i < r
    vector<int> nonzero;

    for (int i = 0; i < r; i++) {
        p[i] = ask(i, r);
        if (p[i] != 0) nonzero.push_back(i);
    }

    auto output_minus_one = [&]() {
        cout << "! -1\n";
        cout.flush();
        exit(0);
    };

    if (N == 2) {
        vector<pair<int,int>> cand;
        for (int lead = 1; lead <= 9; lead++) {
            for (int d0 = 0; d0 <= 9; d0++) {
                if (1LL * lead * d0 == p[0]) {
                    cand.push_back({d0, lead});
                }
            }
        }
        if ((int)cand.size() != 1) output_minus_one();

        cout << "! " << cand[0].second << cand[0].first << '\n';
        cout.flush();
        return 0;
    }

    if ((int)nonzero.size() < 2) {
        output_minus_one();
    }

    int a = nonzero[0];
    int b = nonzero[1];
    long long q = ask(a, b);

    vector<int> ans(N, -1);
    vector<vector<int>> all;

    for (int lead = 1; lead <= 9; lead++) {
        vector<int> d(N, -1);
        d[r] = lead;
        bool ok = true;

        for (int i = 0; i < r; i++) {
            if (p[i] % lead != 0) {
                ok = false;
                break;
            }
            d[i] = (int)(p[i] / lead);
            if (d[i] < 0 || d[i] > 9) {
                ok = false;
                break;
            }
        }

        if (!ok) continue;

        if (1LL * d[a] * d[b] != q) continue;

        all.push_back(d);
    }

    if ((int)all.size() != 1) {
        output_minus_one();
    }

    ans = all[0];

    string s;
    for (int i = N - 1; i >= 0; i--) {
        s.push_back(char('0' + ans[i]));
    }

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