結果

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

ソースコード

diff #
raw source code

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

using namespace std;

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

    vector<int> P(N - 1);
    for (int i = 0; i < N - 1; ++i) {
        cout << "? " << i << " " << N - 1 << endl;
        cin >> P[i];
        if (P[i] == -1) return 0;
    }

    vector<string> cand;
    for (int k = 1; k <= 9; ++k) {
        string s = "";
        bool ok = true;
        for (int i = 0; i < N - 1; ++i) {
            if (P[i] % k != 0 || P[i] / k > 9) {
                ok = false;
                break;
            }
            s += (char)('0' + (P[i] / k));
        }
        if (ok) {
            string x = "";
            x += (char)('0' + k);
            for (int i = N - 2; i >= 0; --i) x += s[i];
            cand.push_back(x);
        }
    }

    if (cand.empty()) {
        cout << "! -1" << endl;
    } else if (cand.size() == 1) {
        cout << "! " << cand[0] << endl;
    } else {
        int best_a = -1, best_b = -1;
        for (int a = 0; a < N; ++a) {
            for (int b = a + 1; b < N; ++b) {
                set<int> products;
                for (const string& s : cand) {
                    int d_a = s[N - 1 - a] - '0';
                    int d_b = s[N - 1 - b] - '0';
                    products.insert(d_a * d_b);
                }
                if (products.size() == cand.size()) {
                    best_a = a;
                    best_b = b;
                    break;
                }
            }
            if (best_a != -1) break;
        }

        if (best_a != -1) {
            cout << "? " << best_a << " " << best_b << endl;
            int final_p;
            cin >> final_p;
            for (const string& s : cand) {
                int d_a = s[N - 1 - best_a] - '0';
                int d_b = s[N - 1 - best_b] - '0';
                if (d_a * d_b == final_p) {
                    cout << "! " << s << endl;
                    return 0;
                }
            }
        }
        cout << "! -1" << endl;
    }

    return 0;
}
0