結果
問題 |
No.167 N^M mod 10
|
ユーザー |
|
提出日時 | 2016-04-11 11:35:25 |
言語 | Java (openjdk 23) |
結果 |
WA
|
実行時間 | - |
コード長 | 695 bytes |
コンパイル時間 | 1,787 ms |
コンパイル使用メモリ | 74,884 KB |
実行使用メモリ | 58,220 KB |
最終ジャッジ日時 | 2024-10-04 06:02:16 |
合計ジャッジ時間 | 14,070 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | WA * 2 |
other | AC * 8 WA * 15 TLE * 4 |
ソースコード
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; } }