結果

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

ソースコード

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 leadPos = N - 1;                 // most significant digit, nonzero
    vector<long long> p(N, -1);          // p[i] = digit[i] * digit[leadPos] for i < leadPos

    for (int i = 0; i < leadPos; i++) {
        p[i] = ask(i, leadPos);
    }

    vector<int> nonzero;
    for (int i = 0; i < leadPos; i++) {
        if (p[i] != 0) nonzero.push_back(i);
    }

    // If fewer than 2 nonzero lower digits exist, one extra query cannot resolve the scale.
    if ((int)nonzero.size() < 2) {
        cout << "! -1\n";
        cout.flush();
        return 0;
    }

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

    vector<vector<int>> cand;

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

        for (int i = 0; i < leadPos; 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;

        cand.push_back(d);
    }

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

    // Build the answer as a string: most significant digit first
    string X;
    for (int i = N - 1; i >= 0; i--) {
        X.push_back(char('0' + cand[0][i]));
    }

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