結果

問題 No.981 一般冪乗根
ユーザー fumofumofunifumofumofuni
提出日時 2022-07-18 01:57:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,864 bytes
コンパイル時間 2,334 ms
コンパイル使用メモリ 212,296 KB
実行使用メモリ 10,264 KB
最終ジャッジ日時 2023-09-12 13:22:34
合計ジャッジ時間 19,565 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
関数 ‘ll modpow(ll, ll, ll)’ 内,
    inlined from ‘void solve()’ at main.cpp:125:16:
main.cpp:48:12: 警告: ‘x’ may be used uninitialized [-Wmaybe-uninitialized]
   48 |   while (n > 0) {
      |          ~~^~~
main.cpp: 関数 ‘void solve()’ 内:
main.cpp:106:6: 備考: ‘x’ はここで定義されています
  106 |   ll x;
      |      ^

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=(n)-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[8]={1,0,-1,0,1,1,-1,-1};
const ll dx[8]={0,1,0,-1,1,-1,1,-1};
template <typename T> inline bool chmax(T &a, T b) {
  return ((a < b) ? (a = b, true) : (false));
}
template <typename T> inline bool chmin(T &a, T b) {
  return ((a > b) ? (a = b, true) : (false));
}
map<ll,ll> factor(long long n) {
    map<ll,ll> ret;
    ll t=n;
    for (long long i = 2; i * i <= n; i++) {
        while (t % i == 0) {
            ret[i]++;t/=i;
        }
    }
    if(t!=1)ret[t]++;
    return ret;
}

ll modpow(ll a,ll n, ll mod) {
  a%=mod;if(a==0)return 0;
  ll res = 1;
  while (n > 0) {
      if (n & 1) res = res * a % mod;
      a = a * a % mod;
      n >>= 1;
  }
  return res;
}
random_device rd; mt19937 mt(rd());
ll primitive_root(ll n){
  uniform_int_distribution<> rand(2,n-1);
  auto f=factor(n-1);
  while(1){
    ll r=rand(mt);
    bool ok=true;
    for(auto q:f){
      if(modpow(r,(n-1)/q.first,n)==1)ok=false;
    }
    if(ok)return r;
  }
}
ll euler_phi(ll n) {
  ll ret = n;
  for(ll i = 2; i * i <= n; i++) {
    if(n % i == 0) {
      ret -= ret / i;
      while(n % i == 0) n /= i;
    }
  }
  if(n > 1) ret -= ret / n;
  return ret;
}


void solve(){
  ll p,k,a;cin >> p >> k >> a;
  /*if(gcd(k,p-1)!=1){
    cout << -1 << endl;return;
  }*/
  ll e=0;
  ll r=primitive_root(p);
  {
    unordered_map<ll,ll> mp;
    ll now=1;
    for(ll i=1;i<50000;i++){
      now=now*r%p;mp[now]=i;
    }
    now=now*r%p;
    ll tmp=1;
    for(ll i=0;i<=50000;i++){
      ll ap=modpow(tmp,p-2,p)*a%p;
      if(mp.count(ap)){
        e=mp[ap]+i*50000;break;
      }
    }
  }
  //cout << r <<" " << e << endl;
  //cout << modpow(r,e,p) << endl;
  //x*k=e mod p-1 を求める
  ll x;
  {
    unordered_map<ll,ll> mp;
    ll now=0;
    for(ll i=0;i<32000;i++){
      mp[now]=i;
      now=(now+k)%(p-1);
    }
    ll tmp=0;
    for(ll i=0;i<32000;i++){
      ll ap=(e-tmp+(p-1))%(p-1);
      if(mp.count(ap)){
        x=i*50000+mp[ap];
        break;
      }
      tmp=(tmp+now)%(p-1);
    }
  }
  //cout << x << endl;
  ll ans=modpow(r,x,p);
  if(modpow(ans,k,p)!=a)cout << -1 << endl;
  else cout << ans << endl;
}
int main(){
  ll t;cin >> t;
  while(t--){
    solve();
  }

} 
0