結果
| 問題 |
No.981 一般冪乗根
|
| ユーザー |
tko919
|
| 提出日時 | 2020-04-07 21:26:49 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,727 bytes |
| コンパイル時間 | 1,569 ms |
| コンパイル使用メモリ | 181,056 KB |
| 実行使用メモリ | 11,424 KB |
| 最終ジャッジ日時 | 2024-07-08 02:00:02 |
| 合計ジャッジ時間 | 17,077 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | TLE * 1 -- * 43 |
ソースコード
#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;
}
tko919