結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
54,008 KB
testcase_01 AC 128 ms
54,232 KB
testcase_02 TLE -
testcase_03 TLE -
testcase_04 TLE -
testcase_05 AC 128 ms
54,308 KB
testcase_06 AC 129 ms
54,080 KB
testcase_07 AC 128 ms
54,252 KB
testcase_08 AC 128 ms
54,228 KB
testcase_09 AC 127 ms
54,360 KB
testcase_10 AC 128 ms
54,388 KB
testcase_11 AC 127 ms
54,136 KB
testcase_12 AC 128 ms
54,240 KB
testcase_13 AC 128 ms
54,012 KB
testcase_14 AC 129 ms
56,164 KB
testcase_15 AC 128 ms
54,080 KB
testcase_16 AC 129 ms
54,168 KB
testcase_17 AC 128 ms
54,028 KB
testcase_18 AC 129 ms
54,148 KB
testcase_19 AC 128 ms
54,092 KB
testcase_20 AC 128 ms
54,288 KB
testcase_21 AC 132 ms
54,304 KB
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 AC 145 ms
54,296 KB
testcase_27 TLE -
testcase_28 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;
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)-'0';
		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;
	}
	void tr(Object...o){System.out.println(Arrays.deepToString(o));}
	
}
0