結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
54,228 KB
testcase_01 AC 126 ms
53,968 KB
testcase_02 AC 186 ms
54,764 KB
testcase_03 AC 184 ms
54,548 KB
testcase_04 AC 186 ms
54,372 KB
testcase_05 AC 129 ms
54,268 KB
testcase_06 AC 129 ms
54,236 KB
testcase_07 AC 128 ms
54,112 KB
testcase_08 AC 126 ms
53,988 KB
testcase_09 AC 127 ms
53,988 KB
testcase_10 AC 128 ms
54,200 KB
testcase_11 AC 129 ms
54,288 KB
testcase_12 AC 128 ms
53,900 KB
testcase_13 AC 132 ms
54,256 KB
testcase_14 WA -
testcase_15 AC 128 ms
54,160 KB
testcase_16 AC 128 ms
54,336 KB
testcase_17 AC 128 ms
54,216 KB
testcase_18 AC 128 ms
54,052 KB
testcase_19 AC 129 ms
54,536 KB
testcase_20 AC 130 ms
54,040 KB
testcase_21 AC 130 ms
54,012 KB
testcase_22 AC 196 ms
54,208 KB
testcase_23 AC 181 ms
54,356 KB
testcase_24 AC 196 ms
54,256 KB
testcase_25 AC 180 ms
54,464 KB
testcase_26 WA -
testcase_27 AC 197 ms
54,520 KB
testcase_28 AC 181 ms
54,296 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;
import java.util.*;
import java.math.*;
public class Main{
	public static void main(String[] args)throws Exception{
		new Main().sovle();
	}
	//0   0,0,0,0,
	//1   1,1,1,1,
	//2   2,4,8,6,
	//3   3,9,7,1,
	//4   4,6,4,6,
	//5   5,5,5,5,
	//6   6,6,6,6,
	//7   7,9,3,1,
	//8   8,4,2,6,
	//9   9,1,9,2,
	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);
		m=m.mod(BigInteger.valueOf(4)).add(BigInteger.valueOf(4));
		//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