結果

問題 No.981 一般冪乗根
ユーザー 👑 kmjpkmjp
提出日時 2020-02-12 01:38:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,160 bytes
コンパイル時間 2,024 ms
コンパイル使用メモリ 177,380 KB
実行使用メモリ 10,548 KB
最終ジャッジ日時 2023-07-30 20:33:40
合計ジャッジ時間 17,723 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

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

#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;

inline int mulmod(int a,int b,int mo) {
	int d,r;
	if(a==0 || b==0) return 0;
	if(a==1 || b==1) return max(a,b);
	__asm__("mull %4;"
	        "divl %2"
		: "=d" (r), "=a" (d)
		: "r" (mo), "a" (a), "d" (b));
	return r;
}

ll modpow(ll a, ll n = mo-2, int m=mo) {
	ll r=1;
	int b=a%m;
	while(n) r=mulmod(r,(n%2)?b:1,m),b=mulmod(b,b,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;
			break;
		}
		if(ng==0) break;
		g++;
	}
	int cur=a;
	int rg=modpow(g);
	int mstep=sqrt(mo);
	unordered_map<int,int> M;
	int i;
	FOR(i,mstep+3) {
		M[cur]=i;
		cur=mulmod(cur,rg,mo);
	}
	int 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=mulmod(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