結果

問題 No.251 大きな桁の復習問題(1)
ユーザー 37zigen37zigen
提出日時 2016-05-03 20:02:35
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,127 bytes
コンパイル時間 2,430 ms
コンパイル使用メモリ 77,752 KB
実行使用メモリ 54,352 KB
最終ジャッジ日時 2024-04-15 11:01:34
合計ジャッジ時間 15,776 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 137 ms
41,792 KB
testcase_01 AC 135 ms
41,488 KB
testcase_02 AC 139 ms
41,664 KB
testcase_03 AC 137 ms
41,568 KB
testcase_04 WA -
testcase_05 AC 136 ms
41,788 KB
testcase_06 WA -
testcase_07 AC 133 ms
41,928 KB
testcase_08 AC 135 ms
41,264 KB
testcase_09 AC 924 ms
47,528 KB
testcase_10 AC 1,004 ms
47,640 KB
testcase_11 AC 896 ms
47,892 KB
testcase_12 AC 908 ms
47,720 KB
testcase_13 AC 907 ms
47,892 KB
testcase_14 AC 903 ms
47,040 KB
testcase_15 AC 889 ms
47,772 KB
testcase_16 AC 920 ms
47,684 KB
testcase_17 AC 909 ms
47,684 KB
testcase_18 AC 901 ms
47,708 KB
testcase_19 AC 890 ms
47,732 KB
testcase_20 AC 138 ms
41,608 KB
testcase_21 AC 140 ms
41,648 KB
testcase_22 AC 135 ms
40,064 KB
testcase_23 AC 133 ms
41,752 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;
import java.math.BigInteger;
import java.util.Scanner;
public class Main{
	public static void main(String[] args)throws Exception{
		new Main().solve();
	}
	final long mod=129402307;
	void solve(){
		Scanner sc=new Scanner(System.in);
		BigInteger n=new BigInteger(sc.next());
		BigInteger m=new BigInteger(sc.next());
		BigInteger ans=pow(n,m,mod);
		System.out.println(ans);
		//n^m mod p を求める。
	}
	
	/**
	 * A^N mod p
	 * 
	 * 1.フェルマーの小定理を用いてNをp-1以下にする
	 * 2.繰り返し二乗法
	 * 
	 */
	BigInteger pow(BigInteger A,BigInteger n,long p){
		BigInteger ans=BigInteger.ONE;
		n=n.remainder(BigInteger.valueOf(p-1));
		while(n.compareTo(BigInteger.ONE)>=0){
			if(n.remainder(BigInteger.valueOf(2)).compareTo(BigInteger.valueOf(0))==0){
				A=A.multiply(A);
				A=A.remainder(BigInteger.valueOf(p));
				n=n.divide(BigInteger.valueOf(2));
			}else if(n.remainder(BigInteger.valueOf(2)).compareTo(BigInteger.valueOf(1))==0){
				ans=ans.multiply(A);
				ans=ans.remainder(BigInteger.valueOf(p));
				n=n.subtract(BigInteger.ONE);
			}
		}
		return ans;
	}
}
0