結果

問題 No.981 一般冪乗根
ユーザー 37zigen37zigen
提出日時 2020-02-04 10:11:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 71 ms / 6,000 ms
コード長 2,839 bytes
コンパイル時間 1,042 ms
コンパイル使用メモリ 96,276 KB
実行使用メモリ 8,960 KB
最終ジャッジ日時 2024-04-18 03:09:54
合計ジャッジ時間 96,012 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
8,832 KB
testcase_01 AC 4 ms
8,832 KB
testcase_02 AC 4 ms
8,832 KB
testcase_03 AC 3 ms
8,960 KB
testcase_04 AC 3 ms
8,832 KB
testcase_05 AC 3 ms
8,960 KB
testcase_06 AC 3 ms
8,960 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 3 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 3 ms
5,376 KB
testcase_18 AC 4 ms
5,376 KB
testcase_19 AC 3 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 3 ms
5,376 KB
testcase_23 AC 3 ms
5,376 KB
testcase_24 AC 3 ms
5,376 KB
testcase_25 AC 4 ms
5,376 KB
testcase_26 AC 4 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 71 ms
5,376 KB
evil_60bit1.txt TLE -
evil_60bit2.txt TLE -
evil_60bit3.txt TLE -
evil_hack AC 2 ms
5,376 KB
evil_hard_random RE -
evil_hard_safeprime.txt RE -
evil_hard_tonelli0 RE -
evil_hard_tonelli1 RE -
evil_hard_tonelli2 RE -
evil_hard_tonelli3 RE -
evil_sefeprime1.txt TLE -
evil_sefeprime2.txt TLE -
evil_sefeprime3.txt TLE -
evil_tonelli1.txt TLE -
evil_tonelli2.txt TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cassert>
#include <cmath>
#include <cstring>

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <stack>
#include <queue>

#include <tuple>

#define getchar getchar_unlocked
#define putchar putchar_unlocked

#define _rep(_1, _2, _3, _4, name, ...) name
#define rep2(i, n) rep3(i, 0, n)
#define rep3(i, a, b) rep4(i, a, b, 1)
#define rep4(i, a, b, c) for (int i = int(a); i < int(b); i += int(c))
#define rep(...) _rep(__VA_ARGS__, rep4, rep3, rep2, _)(__VA_ARGS__)

using namespace std;

using i64 = long long;
using u8 = unsigned char;
using u32 = unsigned;
using u64 = unsigned long long;
using f80 = long double;

vector< pair<int, int> > factors(int n) {
  vector< pair<int, int> > ret;
  for (int i = 2; i64(i) * i <= n; ++i) {
    if (n % i == 0) {
      int e = 1; n /= i;
      while (n % i == 0) n /= i, ++e;
      ret.emplace_back(i, e);
    }
  }
  if (n > 1) ret.emplace_back(n, 1);
  return ret;
}

int gcd(int a, int b) {
  return b ? gcd(b, a % b) : a;
}

int pow_mod(int b, int e, int p) {
  int ret = 1;
  for (; e; e >>= 1, b = i64(b) * b % p) {
    if (e & 1) ret = i64(ret) * b % p;
  }
  return ret;
}

int mod_inv(int a, int mod) {
  int b = mod, s = 1, u = 0;
  while (b) {
    int q = a / b;
    swap(b, a %= b);
    swap(s -= q * u, u);
  }
  if (a != 1) assert(0);
  return s < 0 ? s + mod : s;
}

int msqrtp_p(int a, int p, int e, int mod) {
  int q = mod - 1, s = 0;
  while (q % p == 0) q /= p, ++s;
  int pe = 1;
  rep(_, e) pe *= p;
  int d = mod_inv(pe - q % pe, pe) * q;
  int r = pow_mod(a, (d + 1) / pe, mod);
  int t = pow_mod(a, d, mod);
  if (t == 1) return r;
  int ps = 1; rep(_, s - 1) ps *= p;
  int c = -1;
  for (int z = 2; ; ++z) {
    c = pow_mod(z, q, mod);
    if (pow_mod(c, ps, mod) != 1) break;
  }
  int b = -1;
  while (t != 1) {
    int tmp = pow_mod(t, p, mod), s2 = 1;
    while (tmp != 1) tmp = pow_mod(tmp, p, mod), ++s2;
    if (s2 + e <= s) {
      b = c;
      rep(_, s - s2 - e) b = pow_mod(b, p, mod);
      c = pow_mod(b, pe, mod);
      s = s2;
    }
    r = i64(r) * b % mod;
    t = i64(t) * c % mod;
  }
  return r;
}

int msqrtn_p(int a, int n, int p) {
  assert(n >= 1);
  a %= p, n %= p - 1;
  if (a <= 1) return a;
  int g = gcd(p - 1, n);
  if (pow_mod(a, (p - 1) / g, p) != 1) return -1;
  a = pow_mod(a, mod_inv(n / g, (p - 1) / g), p);
  for (auto pp : factors(g)) {
    a = msqrtp_p(a, pp.first, pp.second, p);
  }
  return a;
}

void solve() {
  int T;scanf("%d", &T);
  for(int i=0;i<T;++i) {
    long long p, k, a; scanf("%d %d %d", &p, &k, &a);
    long x = msqrtn_p(a,k,p);
    printf("%d\n", x);
  }
}

int main() {
  clock_t beg = clock();
  solve();
  clock_t end = clock();
  fprintf(stderr, "%.3f sec\n", double(end - beg) / CLOCKS_PER_SEC);
  return 0;
}
0