結果

問題 No.3501 Digit Products 2
コンテスト
ユーザー sient
提出日時 2026-04-19 00:56:23
言語 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  
実行時間 21 ms / 2,000 ms
コード長 1,978 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,067 ms
コンパイル使用メモリ 341,552 KB
実行使用メモリ 30,332 KB
平均クエリ数 10.89
最終ジャッジ日時 2026-04-19 00:56:34
合計ジャッジ時間 8,951 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 72
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

long long ask(int a, int b) {
    cout << "? " << a << ' ' << b << '\n';
    cout.flush();
    long long x;
    cin >> x;
    if (x == -1) exit(0);
    return x;
}

void answer_num(const string& s) {
    cout << "! " << s << '\n';
    cout.flush();
    exit(0);
}

void answer_minus_one() {
    cout << "! -1\n";
    cout.flush();
    exit(0);
}

long long isqrt_ll(long long x) {
    long long r = sqrt((long double)x);
    while ((r + 1) * (r + 1) <= x) ++r;
    while (r * r > x) --r;
    return r;
}

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

    int N;
    cin >> N;

    if (N == 1) {
        answer_minus_one();
    }

    vector<vector<pair<int,int>>> fac(82);
    for (int a = 1; a <= 9; ++a) {
        for (int b = 1; b <= 9; ++b) {
            fac[a * b].push_back({a, b}); // ordered pair (other digit, msd)
        }
    }

    int msd = N - 1;
    vector<long long> p(N, 0);
    vector<int> pos;

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

    vector<int> d(N, 0);

    if ((int)pos.size() >= 2) {
        int a = pos[0], b = pos[1];
        long long pab = ask(a, b);

        long long sq = p[a] * p[b] / pab;
        long long lead = isqrt_ll(sq);

        d[msd] = (int)lead;
        for (int i = 0; i < N - 1; ++i) {
            d[i] = (int)(p[i] / lead);
        }

        string ans;
        for (int i = N - 1; i >= 0; --i) ans.push_back(char('0' + d[i]));
        answer_num(ans);
    }

    if ((int)pos.size() == 1) {
        int i = pos[0];
        if ((int)fac[p[i]].size() == 1) {
            auto [x, y] = fac[p[i]][0];
            d[i] = x;
            d[msd] = y;

            string ans;
            for (int j = N - 1; j >= 0; --j) ans.push_back(char('0' + d[j]));
            answer_num(ans);
        } else {
            answer_minus_one();
        }
    }

    answer_minus_one();
    return 0;
}
0