結果

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

ソースコード

diff #
raw source code

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
  int n;
  cin >> n;
  auto query = [](int a, int b) -> int
  {
    if (a > b) swap(a, b);
    cout << "? " << a << ' ' << b << endl;
    int res;
    cin >> res;
    return res;
  };
  vector<int> val(n), ans(n);
  for (int i = 0; i < n - 1; ++i) val[i] = query(i, n - 1);
  int id1 = -1, id2 = -1;
  for (int i = 0; i < n - 1; ++i)
  {
    if (val[i] > 0)
    {
      id1 = i;
      break;
    }
  }
  for (int i = n - 2; i >= 0; --i)
  {
    if (val[i] > 0)
    {
      id2 = i;
      break;
    }
  }
  if (id1 == -1 || id2 == -1)
  {
    cout << "! -1" << endl;
    return 0;
  }
  else if (id1 == id2)
  {
    int cnt = 0;
    for (int v1 = 1; v1 <= 9; ++v1)
    {
      if (val[id1] % v1 == 0 && val[id1] / v1 <= 9) ++cnt;
    }
    if (cnt > 1)
    {
      cout << "! -1" << endl;
      return 0;
    }
    for (int v1 = 1; v1 <= 9; ++v1)
    {
      if (val[id1] % v1 == 0 && val[id1] / v1 <= 9)
      {
        ans[n - 1] = v1;
        ans[id1] = val[id1] / v1;
        break;
      }
    }
    cout << "! ";
    for (int i = n - 1; i >= 0; --i) cout << ans[i];
    cout << endl;
    return 0;
  }
  int tmp = query(id1, id2);
  int tmpv = val[id1] * val[id2] / tmp;
  for (int i = 1; i <= 9; ++i)
  {
    if (i * i == tmpv)
    {
      ans[n - 1] = i;
      break;
    }
  }
  for (int i = 0; i < n - 1; ++i) ans[i] = val[i] / ans[n - 1];
  cout << "! ";
  for (int i = n - 1; i >= 0; --i) cout << ans[i];
  cout << endl;
}
0