結果

問題 No.981 一般冪乗根
ユーザー 👑 kmjpkmjp
提出日時 2020-02-12 02:09:58
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,885 bytes
コンパイル時間 2,365 ms
コンパイル使用メモリ 175,496 KB
実行使用メモリ 10,144 KB
最終ジャッジ日時 2024-04-18 07:28:39
合計ジャッジ時間 19,192 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5,808 ms
10,012 KB
testcase_01 AC 5,596 ms
9,888 KB
testcase_02 AC 5,555 ms
10,012 KB
testcase_03 AC 5,908 ms
10,016 KB
testcase_04 AC 5,483 ms
10,144 KB
testcase_05 AC 146 ms
10,144 KB
testcase_06 AC 146 ms
10,144 KB
testcase_07 AC 156 ms
10,012 KB
testcase_08 AC 150 ms
10,016 KB
testcase_09 AC 145 ms
10,140 KB
testcase_10 AC 151 ms
10,144 KB
testcase_11 AC 155 ms
10,012 KB
testcase_12 AC 151 ms
10,144 KB
testcase_13 AC 148 ms
6,944 KB
testcase_14 AC 147 ms
6,944 KB
testcase_15 AC 149 ms
6,944 KB
testcase_16 AC 146 ms
6,948 KB
testcase_17 AC 149 ms
6,944 KB
testcase_18 AC 146 ms
6,940 KB
testcase_19 AC 147 ms
6,944 KB
testcase_20 AC 144 ms
6,940 KB
testcase_21 AC 143 ms
6,940 KB
testcase_22 AC 145 ms
6,944 KB
testcase_23 AC 143 ms
6,944 KB
testcase_24 AC 145 ms
6,944 KB
testcase_25 AC 5,801 ms
6,940 KB
testcase_26 TLE -
testcase_27 AC 6 ms
6,940 KB
testcase_28 AC 5,177 ms
6,940 KB
evil_60bit1.txt TLE -
evil_60bit2.txt TLE -
evil_60bit3.txt TLE -
evil_hack AC 4 ms
6,944 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 #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------


int T;
ll mo;
unordered_map<int,int> M;

ll modpow(ll a, ll n = mo-2, ll m=mo) {
	ll r=1;
	while(n) r=r*((n%2)?a:1)%m,a=a*a%m,n>>=1;
	return r;
}

int totient(int v) {
	int ret=v;
	for(int i=2;i*i<=v;i++) if(v%i==0) {
		ret=ret/i*(i-1);
		while(v%i==0) v/=i;
	}
	if(v>1) ret=ret/v*(v-1);
	return ret;
}

int mod_root(int p,int a) { // x^p=a mod mo
	vector<int> D;
	for(int i=2;i*i<=mo-1;i++) if((mo-1)%i==0) D.push_back(i),D.push_back((mo-1)/i);
	int g=2;
	while(1) {
		int ng=0;
		FORR(d,D) if(modpow(g,d)==1) ng=1;
		if(ng==0) break;
		g++;
	}
	M.clear();
	ll cur=a;
	int rg=modpow(g);
	int mstep=sqrt(mo);
	int i;
	FOR(i,mstep+3) {
		M[cur]=i;
		cur=cur*rg%mo;
	}
	ll pg=modpow(g,mstep);
	int x=-1,step=0;
	cur=1;
	while(x==-1) {
		if(M.count(cur)) x=step+M[cur];
		M[cur]=step;
		cur=cur*pg%mo;
		step+=mstep;
	}
	// g^x=aなのでg^(p*q)=g^x=aとしてq=x/p (mod mo-1) mo-1は合成数なのでGCDで割って対応
	int tmo=mo-1;
	int gcd=__gcd(tmo,p);
	if(x%gcd) return -1;
	tmo/=gcd;
	x/=gcd;
	p/=gcd;
	return modpow(g,1LL*x*modpow(p,totient(tmo)-1,tmo)%tmo);
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>T;
	while(T--) {
		int P,K,A;
		cin>>P>>K>>A;
		mo=P;
		cout<<mod_root(K,A)<<endl;
	}
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0