結果

問題 No.167 N^M mod 10
ユーザー tnoda_
提出日時 2015-04-02 16:01:36
言語 Go
(1.23.4)
結果
AC  
実行時間 24 ms / 1,000 ms
コード長 855 bytes
コンパイル時間 14,473 ms
コンパイル使用メモリ 219,208 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-09-22 01:13:10
合計ジャッジ時間 15,613 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"fmt"
	"strconv"
)

func lastDigit(s string) int {
	return int(s[len(s)-1] - '0')
}

func mod4(s string) int {
	l := len(s)
	switch l {
	case 1:
		s = s[l-1:]
	default:
		s = s[l-2:]
	}
	x, _ := strconv.Atoi(s)
	return x % 4
}

func solve() int {
	var N, M string
	fmt.Scan(&N, &M)
	if M == "0" {
		return 1
	}
	n := lastDigit(N)
	if M == "1" {
		return n
	}
	switch n {
	case 0, 1, 5, 6:
		return n
	case 2:
		pat := []int{6, 2, 4, 8}
		return pat[mod4(M)]
	case 3:
		pat := []int{1, 3, 9, 7}
		return pat[mod4(M)]
	case 4:
		pat := []int{6, 4, 6, 4}
		return pat[mod4(M)]
	case 7:
		pat := []int{1, 7, 9, 3}
		return pat[mod4(M)]
	case 8:
		pat := []int{6, 8, 4, 2}
		return pat[mod4(M)]
	case 9:
		pat := []int{1, 9, 1, 9}
		return pat[mod4(M)]
	default:
		panic("do not reach here")
	}
}

func main() {
	fmt.Println(solve())
}
0