結果

問題 No.3501 Digit Products 2
コンテスト
ユーザー D M
提出日時 2026-05-20 11:33:33
言語 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
結果
AC  
実行時間 55 ms / 2,000 ms
コード長 1,744 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,004 ms
コンパイル使用メモリ 338,112 KB
実行使用メモリ 30,064 KB
平均クエリ数 10.89
最終ジャッジ日時 2026-05-20 11:33:46
合計ジャッジ時間 8,511 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
純コード判定待ち
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 72
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

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

    ll n;
    cin >> n;

    vector<ll> P(n, 0);      // P[i] = digit[i] * digit[n-1]
    vector<ll> digit(n, 0);  // digit[i] = 10^i の位

    auto ask = [&](ll a, ll b) -> ll {
        cout << "? " << a << " " << b << endl;
        ll p;
        cin >> p;
        if (p == -1) exit(0);
        return p;
    };

    vector<int> pos;

    for (int i = 0; i < n - 1; i++) {
        P[i] = ask(i, n - 1);
        if (P[i] > 0) pos.push_back(i);
    }

    if (pos.empty()) {
        cout << "! -1" << endl;
        return 0;
    }

    if (pos.size() == 1) {
        int k = pos[0];
        ll p = P[k];

        vector<pair<int, int>> cand; // {最上位桁, k桁目}
        for (int msd = 1; msd <= 9; msd++) {
            for (int x = 1; x <= 9; x++) {
                if (msd * x == p) {
                    cand.push_back({msd, x});
                }
            }
        }

        if (cand.size() != 1) {
            cout << "! -1" << endl;
            return 0;
        }

        digit[n - 1] = cand[0].first;
        digit[k] = cand[0].second;

        cout << "! ";
        for (int i = n - 1; i >= 0; i--) cout << digit[i];
        cout << endl;
        return 0;
    }

    int i = pos[0];
    int j = pos[1];

    ll q = ask(i, j); // digit[i] * digit[j]

    ll sq = P[i] * P[j] / q; // digit[n-1]^2

    int msd = -1;
    for (int d = 1; d <= 9; d++) {
        if (d * d == sq) msd = d;
    }

    digit[n - 1] = msd;

    for (int k = 0; k < n - 1; k++) {
        digit[k] = P[k] / msd;
    }

    cout << "! ";
    for (int k = n - 1; k >= 0; k--) cout << digit[k];
    cout << endl;
}
0