結果

問題 No.981 一般冪乗根
ユーザー 37zigen37zigen
提出日時 2020-02-05 13:15:48
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,973 bytes
コンパイル時間 2,565 ms
コンパイル使用メモリ 81,744 KB
実行使用メモリ 59,196 KB
最終ジャッジ日時 2024-04-17 20:03:36
合計ジャッジ時間 55,021 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 281 ms
45,240 KB
testcase_26 AC 282 ms
45,128 KB
testcase_27 WA -
testcase_28 AC 853 ms
46,620 KB
evil_60bit1.txt WA -
evil_60bit2.txt WA -
evil_60bit3.txt WA -
evil_hack WA -
evil_hard_random WA -
evil_hard_safeprime.txt WA -
evil_hard_tonelli0 WA -
evil_hard_tonelli1 WA -
evil_hard_tonelli2 WA -
evil_hard_tonelli3 WA -
evil_sefeprime1.txt WA -
evil_sefeprime2.txt WA -
evil_sefeprime3.txt WA -
evil_tonelli1.txt WA -
evil_tonelli2.txt WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

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;
		a=pow(a,inv(k/g,(p-1)/g),p);
		for(long div=2;div*div<=g;++div) {
			int sz=0;
			while(g%div==0) {
				g/=div;
				++sz;
			}
			if(sz>0) {
				long b=peth_root(a,div,sz,p);
				a=b;
			}
		}
		if(g>1)a=peth_root(a,g,1,p);
		return a;
	}
	
	// (p^e)x=a in Z/(p^s)Z×Z/qZ
	// mod=q(p^s)
	long peth_root(long a,long p,int e,long mod) {
		long q=mod-1;
		int s=0;
		while(q%p==0){q/=p;++s;}
		long ans=pow(a,(inv(q,p)*(p-1)%p*q+1)/p,mod);
		long c=2;
		while(pow(c,(mod-1)/p,mod)==1)++c;
		c=pow(c,q,mod);
		while(true){
			long err=pow(ans,p,mod)*inv(a,mod)%mod;
			int bit=s-cnt(err,p,mod);
			if(bit==s)break;
			ans=ans*pow(c,pow(p,bit-1,mod),mod)%mod;
		}
		return ans;
	}
	
	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