結果

問題 No.3501 Digit Products 2
コンテスト
ユーザー Saku0512
提出日時 2026-04-18 01:52:01
言語 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  
実行時間 27 ms / 2,000 ms
コード長 1,467 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,376 ms
コンパイル使用メモリ 340,284 KB
実行使用メモリ 30,320 KB
平均クエリ数 10.47
最終ジャッジ日時 2026-04-18 01:52:16
合計ジャッジ時間 9,119 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 72
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

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

void ans(string s) {
    cout << "! " << s << endl;
    exit(0);
}

int main() {
    int N;
    if (!(cin >> N)) return 0;

    if (N == 2) {
        int p = ask(0, 1);
        string s = "";
        for (int x = 1; x <= 9; ++x) {
            for (int y = 0; y <= 9; ++y) {
                if (x * y == p) {
                    if (s != "") ans("-1");
                    s = to_string(x) + to_string(y);
                }
            }
        }
        ans(s != "" ? s : "-1");
    }

    vector<int> P(N - 1);
    for (int i = 0; i < N - 1; ++i) P[i] = ask(i, N - 1);

    vector<int> S;
    for (int d = 1; d <= 9; ++d) {
        bool ok = true;
        for (int p : P) if (p % d || p / d > 9) ok = false;
        if (ok) S.push_back(d);
    }

    if (S.empty()) ans("-1");

    auto out = [&](int d) {
        string s = to_string(d);
        for (int i = N - 2; i >= 0; --i) s += to_string(P[i] / d);
        ans(s);
    };

    if (S.size() == 1) out(S[0]);

    int u = -1, v = -1;
    for (int i = 0; i < N - 1; ++i) {
        if (P[i] > 0) {
            if (u == -1) u = i;
            else if (v == -1) v = i;
        }
    }

    if (u != -1 && v != -1) {
        int q = ask(u, v);
        for (int d : S) if ((P[u] / d) * (P[v] / d) == q) out(d);
    }
    
    ans("-1");
}
0