結果

問題 No.167 N^M mod 10
ユーザー 37zigen37zigen
提出日時 2016-04-11 11:35:25
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 695 bytes
コンパイル時間 2,249 ms
コンパイル使用メモリ 74,704 KB
実行使用メモリ 58,220 KB
最終ジャッジ日時 2024-04-14 23:58:23
合計ジャッジ時間 16,377 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 127 ms
53,992 KB
testcase_02 TLE -
testcase_03 TLE -
testcase_04 TLE -
testcase_05 WA -
testcase_06 AC 125 ms
54,120 KB
testcase_07 AC 127 ms
54,248 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 127 ms
54,352 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 127 ms
54,184 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 AC 142 ms
54,328 KB
testcase_27 TLE -
testcase_28 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.math.*;
public class Main{
	public static void main(String[] args)throws Exception{
		new Main().sovle();
	}
	void sovle(){
		Scanner sc=new Scanner(System.in);
		String N=sc.next();
		String M=sc.next();
		int n=N.charAt(N.length()-1);
		BigInteger m=new BigInteger(M);
		//n^m mod 10
		System.out.println(pow(n,m,10));
		
	}
	int pow(int n,BigInteger m,int mod){
		int ans=1;
		int pow=n;
		while(m.compareTo(BigInteger.ONE)>=0){
			if(m.mod(BigInteger.valueOf(2)).equals(BigInteger.ZERO)){
				pow*=pow;
				pow%=mod;
				m=m.divide(BigInteger.valueOf(2));
			}else{
				ans*=pow;
				ans%=mod;
				m=m.subtract(BigInteger.ONE);
			}
		}
		return ans;
	}
	
}
0