結果

問題 No.981 一般冪乗根
ユーザー tko919tko919
提出日時 2020-04-07 21:26:49
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,727 bytes
コンパイル時間 1,894 ms
コンパイル使用メモリ 177,088 KB
実行使用メモリ 8,916 KB
最終ジャッジ日時 2023-09-22 09:58:03
合計ジャッジ時間 18,619 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 #

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

//template
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(a);i>(b);i--)
#define ALL(v) (v).begin(),(v).end()
typedef long long int ll;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
void tostr(ll x,string& res){while(x)res+=('0'+(x%10)),x/=10; reverse(ALL(res)); return;}
template<class T> inline bool chmax(T& a,T b){ if(a<b){a=b;return 1;}return 0; }
template<class T> inline bool chmin(T& a,T b){ if(a>b){a=b;return 1;}return 0; }
//end

int mpow(int x,ll t,int m){
   int res=1;
   while(t){
      if(t&1)res=(1LL*res*x)%m;
      x=(1LL*x*x)%m; t>>=1;
   } return res;
}

int phi(int n){
   int res=n;
   for(int i=2;i*i<=n;i++)if(n%i==0){
      res=res/i*(i-1); while(n%i==0)n/=i;
   } if(n!=1)res=res/n*(n-1); return res;
}

mt19937 mt;
int modroot(int k,int y,int m){
   if(m==2)return y&1;
   vector<int> ds; int tmp=m-1;
   for(int p=2;p*p<=tmp;p++)if(tmp%p==0){
      ds.push_back(p); if(p*p!=tmp)ds.push_back(tmp/p);
   }
   int g;
   while(1){
      g=mt()%(m-2)+2; bool f=1;
      for(int d:ds)if(mpow(g,d,m)==1){f=0; break;}
      if(f)break;
   }
   unordered_map<int,int> mp;
   int cur=y,ig=mpow(g,m-2,m),block=sqrt(m);
   rep(i,0,block+2){mp[cur]=i; cur=(cur*ig)%m;}
   int gs=mpow(g,block,m),x=-1,pos=0; cur=1;
   while(x==-1){
      if(mp.count(cur))x=pos+mp[cur];
      cur=(cur*gs)%m; pos+=block;
   }
   int gcd=__gcd(tmp,k); if(x%gcd)return -1;
   tmp/=gcd; x/=gcd; k/=gcd;
   return mpow(g,(1LL*x*mpow(k,phi(tmp)-1,tmp))%tmp,m);
}

int main(){
   int q; cin>>q;
   while(q--){
      int k,y,m; cin>>m>>k>>y;
      cout<<modroot(k,y,m)<<endl;
   }
   return 0;
}
0