結果

問題 No.984 Inversion
ユーザー risujirohrisujiroh
提出日時 2020-02-11 14:25:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 1,886 bytes
コンパイル時間 1,740 ms
コンパイル使用メモリ 178,020 KB
実行使用メモリ 6,548 KB
最終ジャッジ日時 2024-04-08 15:00:53
合計ジャッジ時間 2,969 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 9 ms
6,548 KB
testcase_03 AC 16 ms
6,548 KB
testcase_04 AC 11 ms
6,548 KB
testcase_05 AC 8 ms
6,548 KB
testcase_06 AC 8 ms
6,548 KB
testcase_07 AC 3 ms
6,548 KB
testcase_08 AC 15 ms
6,548 KB
testcase_09 AC 23 ms
6,548 KB
testcase_10 AC 21 ms
6,548 KB
testcase_11 AC 11 ms
6,548 KB
testcase_12 AC 11 ms
6,548 KB
testcase_13 AC 14 ms
6,548 KB
testcase_14 AC 7 ms
6,548 KB
testcase_15 AC 19 ms
6,548 KB
testcase_16 AC 15 ms
6,548 KB
testcase_17 AC 19 ms
6,548 KB
testcase_18 AC 23 ms
6,548 KB
testcase_19 AC 14 ms
6,548 KB
testcase_20 AC 26 ms
6,548 KB
testcase_21 AC 24 ms
6,548 KB
testcase_22 AC 24 ms
6,548 KB
testcase_23 AC 21 ms
6,548 KB
testcase_24 AC 18 ms
6,548 KB
testcase_25 AC 24 ms
6,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template <class T, class F = multiplies<T>>
T power(T a, long long n, F op = multiplies<T>(), T e = {1}) {
  assert(n >= 0);
  while (n) {
    if (n & 1) e = op(e, a);
    if (n >>= 1) a = op(a, a);
  }
  return e;
}

unsigned mod = 2;
struct mint {
  using m = mint;
  unsigned v;
  mint(long long a = 0) : v((a %= mod) < 0 ? a + mod : a) {}
  m operator-() const { return m() -= *this; }
  m& operator+=(m r) { if ((v += r.v) >= mod) v -= mod; return *this; }
  m& operator-=(m r) { if (v < r.v) v += mod; v -= r.v; return *this; }
  m& operator*=(m r) { v = (uint64_t)v * r.v % mod; return *this; }
  m& operator/=(m r) { return *this *= power(r, mod - 2); }
  friend m operator+(m l, m r) { return l += r; }
  friend m operator-(m l, m r) { return l -= r; }
  friend m operator*(m l, m r) { return l *= r; }
  friend m operator/(m l, m r) { return l /= r; }
  friend bool operator==(m l, m r) { return l.v == r.v; }
};

template <class Z> map<Z, int> factorize(Z n) {
  map<Z, int> res;
  for (Z i = 2; i * i <= n; ++i) while (n % i == 0) ++res[i], n /= i;
  if (n != 1) ++res[n];
  return res;
}

int mod_log(mint g, mint x) {
  int m = ceil(sqrt(mod - 1));
  map<int, int> mp;
  mint a = 1;
  for (int j = 0; j < m; ++j) {
    mp[a.v] = j;
    a *= g;
  }
  g = 1 / power(g, m), a = 1;
  for (int i = 0; i < (mod - 1 + m - 1) / m; ++i) {
    if (mp.count((a * x).v)) {
      return i * m + mp[(a * x).v];
    }
    a *= g;
  }
  return -1;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int p, n;
  cin >> p >> n;
  auto mp = factorize(p - 1);
  mod = p;
  for (int g = 1; ; ++g) {
    bool ok = true;
    for (auto e : mp) {
      if (power(mint(g), (p - 1) / e.first) == 1) {
        ok = false;
        break;
      }
    }
    if (ok) {
      cout << (mod_log(g, n) & 1) << '\n';
      exit(0);
    }
  }
}
0