結果

問題 No.3501 Digit Products 2
コンテスト
ユーザー Qiu Tian
提出日時 2026-04-18 15:26:28
言語 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
結果
WA  
実行時間 -
コード長 1,785 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,726 ms
コンパイル使用メモリ 339,968 KB
実行使用メモリ 30,320 KB
平均クエリ数 3.70
最終ジャッジ日時 2026-04-18 15:26:44
合計ジャッジ時間 11,710 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 18 WA * 54
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

long long ask(int a, int b) {
    cout << "? " << a << " " << b << endl;
    cout.flush();

    long long x;
    cin >> x;
    if (x == -1) exit(0);
    return x;
}

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

    int N;
    cin >> N;

    vector<int> d(N);

    int i = 0;
    while (i + 2 < N) {
        long long ab = ask(i, i+1);
        long long ac = ask(i, i+2);
        long long bc = ask(i+1, i+2);

        bool found = false;

        vector<array<int,3>> candidates;

for (int A=0; A<=9; A++)
for (int B=0; B<=9; B++)
for (int C=0; C<=9; C++) {
    if (A*B==ab && A*C==ac && B*C==bc) {
        candidates.push_back({A,B,C});
    }
}

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

d[i]   = candidates[0][0];
d[i+1] = candidates[0][1];
d[i+2] = candidates[0][2];

        done:
        if (!found) {
            cout << "! -1" << endl;
            cout.flush();
            return 0;
        }

        i += 3;
    }

    // Handle remaining digits
    if (i < N) {
        if (i > 0) {
            long long p = ask(i-1, i);
            if (d[i-1] == 0) {
                // ambiguous → cannot determine
                cout << "! -1" << endl;
                cout.flush();
                return 0;
            }
            d[i] = p / d[i-1];
        }
    }

    if (i + 1 < N) {
        long long p = ask(i, i+1);
        if (d[i] == 0) {
            cout << "! -1" << endl;
            cout.flush();
            return 0;
        }
        d[i+1] = p / d[i];
    }

    // Build number
    long long X = 0, pw = 1;
    for (int i = 0; i < N; i++) {
        X += pw * d[i];
        pw *= 10;
    }

    cout << "! " << X << endl;
    cout.flush();
}
0