結果

問題 No.167 N^M mod 10
コンテスト
ユーザー 37zigen
提出日時 2016-04-11 11:58:36
言語 Java
(openjdk 26.0.2.1 + ACL)
コンパイル:
javac -J-Duser.language=en -encoding UTF8 -cp /opt/aclib/ac_library.jar _filename_
実行:
java -ea -Xmx700m -Xss256M -DONLINE_JUDGE=true -cp .:/opt/aclib/ac_library.jar _class_
結果
WA  
実行時間 -
コード長 1,109 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,496 ms
コンパイル使用メモリ 86,236 KB
実行使用メモリ 46,488 KB
最終ジャッジ日時 2026-09-10 01:04:02
合計ジャッジ時間 5,640 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 24 WA * 3
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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();
		BigInteger nn=new BigInteger(N);
		nn=nn.mod(BigInteger.TEN);
		int n=nn.intValue();
		if(n==0){
			System.out.println(1);
			return;
		}
		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