結果

問題 No.251 大きな桁の復習問題(1)
ユーザー 37zigen37zigen
提出日時 2016-05-03 20:09:03
言語 Java21
(openjdk 21)
結果
AC  
実行時間 705 ms / 5,000 ms
コード長 1,261 bytes
コンパイル時間 2,480 ms
コンパイル使用メモリ 74,600 KB
実行使用メモリ 60,660 KB
最終ジャッジ日時 2023-08-22 17:10:49
合計ジャッジ時間 12,612 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
55,656 KB
testcase_01 AC 121 ms
55,680 KB
testcase_02 AC 122 ms
55,500 KB
testcase_03 AC 120 ms
55,716 KB
testcase_04 AC 121 ms
55,680 KB
testcase_05 AC 120 ms
57,452 KB
testcase_06 AC 120 ms
55,372 KB
testcase_07 AC 121 ms
55,852 KB
testcase_08 AC 121 ms
55,776 KB
testcase_09 AC 693 ms
59,408 KB
testcase_10 AC 699 ms
58,956 KB
testcase_11 AC 703 ms
58,904 KB
testcase_12 AC 675 ms
59,084 KB
testcase_13 AC 694 ms
58,660 KB
testcase_14 AC 705 ms
58,944 KB
testcase_15 AC 695 ms
57,260 KB
testcase_16 AC 658 ms
58,328 KB
testcase_17 AC 665 ms
58,724 KB
testcase_18 AC 695 ms
60,660 KB
testcase_19 AC 676 ms
58,904 KB
testcase_20 AC 122 ms
55,676 KB
testcase_21 AC 121 ms
55,756 KB
testcase_22 AC 121 ms
55,804 KB
testcase_23 AC 121 ms
55,740 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){
		A=A.remainder(BigInteger.valueOf(p));
		if(A.compareTo(BigInteger.ZERO)==0&&n.compareTo(BigInteger.ZERO)!=0)return BigInteger.ZERO;
		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