結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 159 ms
41,328 KB
testcase_01 WA -
testcase_02 AC 153 ms
41,320 KB
testcase_03 AC 159 ms
41,596 KB
testcase_04 AC 155 ms
41,220 KB
testcase_05 AC 160 ms
41,420 KB
testcase_06 AC 151 ms
41,368 KB
testcase_07 AC 156 ms
42,008 KB
testcase_08 AC 153 ms
41,624 KB
testcase_09 AC 769 ms
44,796 KB
testcase_10 AC 762 ms
44,548 KB
testcase_11 AC 774 ms
44,220 KB
testcase_12 AC 790 ms
44,416 KB
testcase_13 AC 774 ms
44,384 KB
testcase_14 AC 768 ms
44,588 KB
testcase_15 AC 738 ms
44,528 KB
testcase_16 AC 732 ms
44,336 KB
testcase_17 AC 778 ms
44,612 KB
testcase_18 AC 781 ms
44,416 KB
testcase_19 AC 863 ms
44,336 KB
testcase_20 AC 163 ms
41,332 KB
testcase_21 AC 163 ms
41,276 KB
testcase_22 AC 159 ms
42,100 KB
testcase_23 AC 165 ms
41,440 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)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