結果

問題 No.3501 Digit Products 2
コンテスト
ユーザー Enderaoe Lyther
提出日時 2026-04-17 21:19: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
結果
AC  
実行時間 28 ms / 2,000 ms
コード長 2,179 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,522 ms
コンパイル使用メモリ 167,496 KB
実行使用メモリ 30,052 KB
平均クエリ数 10.89
最終ジャッジ日時 2026-04-17 21:20:00
合計ジャッジ時間 9,553 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 72
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <string>
#include <vector>

using namespace std;

static void answer(const string& s) {
    cout << "! " << s << '\n';
    cout.flush();
}

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

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

    vector<int> q(N - 1);
    for (int i = 0; i < N - 1; ++i) {
        cout << "? " << i << ' ' << N - 1 << '\n';
        cout.flush();
        cin >> q[i];
        if (q[i] == -1) return 0;
    }

    vector<int> pos;
    for (int i = 0; i < N - 1; ++i) {
        if (q[i] > 0) pos.push_back(i);
    }

    auto make_string = [&](const vector<int>& d) {
        string s;
        s.reserve(N);
        for (int i = N - 1; i >= 0; --i) s.push_back(char('0' + d[i]));
        return s;
    };

    if (pos.empty()) {
        answer("-1");
        return 0;
    }

    if (pos.size() == 1) {
        int k = pos[0];
        int prod = q[k];
        string only;
        int cnt = 0;
        for (int low = 1; low <= 9; ++low) {
            for (int high = 1; high <= 9; ++high) {
                if (low * high != prod) continue;
                vector<int> d(N, 0);
                d[k] = low;
                d[N - 1] = high;
                only = make_string(d);
                ++cnt;
            }
        }
        if (cnt == 1) answer(only);
        else answer("-1");
        return 0;
    }

    int a = pos[0];
    int b = pos[1];
    cout << "? " << a << ' ' << b << '\n';
    cout.flush();

    int r;
    cin >> r;
    if (r == -1) return 0;

    string only;
    int cnt = 0;
    for (int top = 1; top <= 9; ++top) {
        if (q[a] * q[b] != top * top * r) continue;
        vector<int> d(N, 0);
        d[N - 1] = top;
        bool ok = true;
        for (int i = 0; i < N - 1; ++i) {
            if (q[i] % top != 0) {
                ok = false;
                break;
            }
            d[i] = q[i] / top;
            if (d[i] < 0 || d[i] > 9) {
                ok = false;
                break;
            }
        }
        if (!ok) continue;
        only = make_string(d);
        ++cnt;
    }

    if (cnt == 1) answer(only);
    else answer("-1");
    return 0;
}
0