結果

問題 No.981 一般冪乗根
ユーザー potetisenseipotetisensei
提出日時 2020-02-07 23:16:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,233 bytes
コンパイル時間 2,148 ms
コンパイル使用メモリ 173,700 KB
実行使用メモリ 187,396 KB
最終ジャッジ日時 2024-04-17 20:39:56
合計ジャッジ時間 16,834 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
#define XX first
#define YY second
#define pb emplace_back
#define FOR(i,a,b) for(int (i)=(a);i<(b);++(i))
#define EFOR(i,a,b) for(int (i)=(a);i<=(b);++(i))
#define rep(X,Y) for (int (X) = 0;(X) < (Y);++(X))
#define REP rep
#define rrep(X,Y) for (int (X) = (Y)-1;(X) >=0;--(X))
#define all(X) (X).begin(),(X).end()
#define eb emplace_back

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef ll LL;
typedef pii PII;
typedef pll PLL;
const ll MOD=1e9+7;

#define rall(X) (X).rbegin(),(X).rend()
#define UNIQUE(X) (X).erase(unique(all(X)),(X).end())
#define reps(X,S,Y) for (int (X) = S;(X) < (Y);++(X))
#define rreps(X,S,Y) for (int (X) = (Y)-1;(X) >= (S);--(X))
template<class T> inline bool MX(T &l,const T &r){return l<r?l=r,1:0;}
template<class T> inline bool MN(T &l,const T &r){return l>r?l=r,1:0;}

int T;

using ULL = unsigned long long;
ULL xorshift(void) {
  static ULL x = 123456789;
  static ULL y = 362436069;
  static ULL z = 521288629;
  static ULL w = 88675123;
  ULL t;
  t = x ^ (x << 11);
  x = y; y = z; z = w;
  return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
}

LL mypow(LL base, LL exp, LL mod) {
  if (exp == 0) return 1;
  LL res = mypow(base*base%mod, exp/2, mod);
  if (exp%2) res = res*base%mod;
  return res;
}

inline ll modinv(ll a, ll m){
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    swap(a -= t * b, b);
    swap(u -= t * v, v);
  }
  return (u % m + m) % m;
}

void Solve() {
  LL p, k, a;
  cin >> p >> k >> a;

  LL n = 1;
  while (n*n < 10*p) n++;
  //n = LL(1e6);
  vector<pll> vs;
  rep(i, n) {
    LL t = 1 + xorshift() % (p-1);
    vs.eb(pll(mypow(t, k, p), t));
  }
  vs.eb(pll(1, 1));
  sort(all(vs));

  for (auto &it : vs) {
    if (it.XX == a) {
      cout << it.YY << endl;
      return;
    }
    LL b = a * modinv(it.XX, p) % p;
    auto itr = lower_bound(all(vs), pll(b, 0));
    if (itr != vs.end() && itr->XX == b) {
      LL ans = itr->YY * it.YY % p;
      assert(mypow(ans, k, p) == a);
      cout << ans << endl;
      return;
    }
  }
  cout << "-1\n";
}

signed main(){
  ios_base::sync_with_stdio(false);
  cout<<fixed<<setprecision(0);

  cin >> T;
  while (T--) {
    Solve();
  }
}
0