結果

問題 No.981 一般冪乗根
ユーザー tko919tko919
提出日時 2020-04-07 21:29:49
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 5,659 ms / 6,000 ms
コード長 1,726 bytes
コンパイル時間 1,767 ms
コンパイル使用メモリ 177,520 KB
実行使用メモリ 8,080 KB
最終ジャッジ日時 2023-09-22 10:15:58
合計ジャッジ時間 197,565 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5,032 ms
7,672 KB
testcase_01 AC 5,010 ms
7,904 KB
testcase_02 AC 5,024 ms
7,600 KB
testcase_03 AC 4,879 ms
8,080 KB
testcase_04 AC 4,990 ms
7,824 KB
testcase_05 AC 149 ms
7,556 KB
testcase_06 AC 150 ms
7,568 KB
testcase_07 AC 150 ms
7,504 KB
testcase_08 AC 151 ms
7,504 KB
testcase_09 AC 149 ms
7,684 KB
testcase_10 AC 149 ms
7,620 KB
testcase_11 AC 150 ms
7,832 KB
testcase_12 AC 149 ms
7,912 KB
testcase_13 AC 150 ms
7,680 KB
testcase_14 AC 150 ms
4,384 KB
testcase_15 AC 151 ms
4,384 KB
testcase_16 AC 150 ms
4,380 KB
testcase_17 AC 150 ms
4,380 KB
testcase_18 AC 150 ms
4,380 KB
testcase_19 AC 149 ms
4,380 KB
testcase_20 AC 149 ms
4,380 KB
testcase_21 AC 149 ms
4,384 KB
testcase_22 AC 150 ms
4,380 KB
testcase_23 AC 151 ms
4,376 KB
testcase_24 AC 151 ms
4,384 KB
testcase_25 AC 5,003 ms
4,624 KB
testcase_26 AC 5,659 ms
5,020 KB
testcase_27 AC 5 ms
4,376 KB
testcase_28 AC 4,711 ms
4,680 KB
evil_60bit1.txt TLE -
evil_60bit2.txt TLE -
evil_60bit3.txt TLE -
evil_hack AC 3 ms
4,380 KB
evil_hard_random TLE -
evil_hard_safeprime.txt TLE -
evil_hard_tonelli0 TLE -
evil_hard_tonelli1 TLE -
evil_hard_tonelli2 TLE -
evil_hard_tonelli3 TLE -
evil_sefeprime1.txt TLE -
evil_sefeprime2.txt TLE -
evil_sefeprime3.txt TLE -
evil_tonelli1.txt TLE -
evil_tonelli2.txt TLE -
権限があれば一括ダウンロードができます

ソースコード

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=2;
   while(1){
      bool f=1;
      for(int d:ds)if(mpow(g,d,m)==1){f=0; break;}
      if(f)break; g++;
   }
   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=(1LL*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=(1LL*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