結果

問題 No.981 一般冪乗根
ユーザー 37zigen37zigen
提出日時 2020-02-01 04:05:06
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,958 bytes
コンパイル時間 2,400 ms
コンパイル使用メモリ 79,432 KB
実行使用メモリ 68,900 KB
最終ジャッジ日時 2024-04-17 19:58:44
合計ジャッジ時間 17,313 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

import java.util.*;
import java.io.*;

class Main {
	public static void main(String[] args) throws Exception {
		new Main().run();
	}

	void run() {
		Scanner sc = new Scanner(System.in);
        int T=sc.nextInt();
        for(int i=0;i<T;++i){
			long p=sc.nextLong();
			long k=sc.nextLong();
			long a=sc.nextLong();
			long root = kth_root(a,k,p);
			System.out.println(root);
        }
	}
	
	long kth_root(long a,long k,long p){
		long g=gcd(k,p-1);
		if(pow(a,(p-1)/g,p)!=1)return -1;
		long root=primitiveRoot(p);
		long y=log(a,root,p);
		long z=inv(k/g,(p-1)/g)*(y/g)%(p-1);
		return pow(root,z,p);
	}
		
	// a=g^x mod p
	long log(long a,long g,long p){
		int v=(int)Math.sqrt(p)+1;
		//g^{qv+r}=a
		HashMap<Long,Integer> map=new HashMap<>();
		long mul=1;
		for(int r=0;r<=v;++r){
			map.put(mul,r);
			mul=mul*g%p;
		}
		long g2=pow(inv(g,p),v,p);
		mul=1;
		for(int q=0;q<=v;++q){
			if(map.containsKey(a*mul%p)){
				return (q*v%(p-1)+map.get(a*mul%p))%(p-1);
			}
			mul=mul*g2%p;
		}
		throw new AssertionError();
	}
	
	long primitiveRoot(long p){
		for(long i=1;i<p;++i){
			if(isPrimitiveRoot(i,p))return i;
		}
		throw new AssertionError();
	}
	
	boolean isPrimitiveRoot(long a,long p){
		boolean ret=true;
		for(long div=2;div*div<=p-1;++div){
			ret&=pow(a,div,p)!=1;
			ret&=pow(a,(p-1)/div,p)!=1;
			if(!ret)return false;
		}
		return true;
	}
	
	int cnt(long a,long base,long p){
		int ret=0;
		while(a!=1){
			a=pow(a,base,p);
			++ret;
		}
		return ret;
	}
	
	long inv(long a,long p){
		a%=p;
		long u=1,v=0;
		long b=p;
		while(b>0) {
			long q=a/b;
			a%=b;
			u-=v*q%p;
			u%=p;
			{
				u^=v;v^=u;u^=v;
				a^=b;b^=a;a^=b;
			}
		}
		return (u%p+p)%p;
	}
	
	long pow(long a,long n,long p){
		n%=p-1;
		long r=1;
		for(;n>0;n>>=1,a=a*a%p)if(n%2==1)r=r*a%p;
		return r;
	}
	
	long gcd(long a,long b) {
		return a==0?b:gcd(b%a,a);
	}
	
	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

}
0