結果

問題 No.167 N^M mod 10
コンテスト
ユーザー yuki2006
提出日時 2015-03-19 19:28:12
言語 Go
(1.25.5)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,137 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 12,496 ms
コンパイル使用メモリ 240,460 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2026-01-01 17:54:52
合計ジャッジ時間 14,050 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

package main

import (
	"bufio"
	"os"
	"strconv"
	"fmt"
)

var s = bufio.NewScanner(os.Stdin)

func next() string {
	// s.Split(bufio.ScanWords) // 削除: Scan後に呼ぶとPanicになるため
	s.Scan()
	return s.Text()
}
func nextLine() string {
	// s.Split(bufio.ScanLines) // 削除: 同上
	s.Scan()
	return s.Text()
}

func nextInt() int {
	i, e := strconv.Atoi(next())
	if e != nil {
		panic(e)
	}
	return i
}
func nextLong() int64 {
	i, e := strconv.ParseInt(next(), 10, 64)
	if e != nil {
		panic(e)
	}
	return i
}

//aのb乗をします O(log b)
func pow(a int, b int) int {
	total := 1
	tmp := a
	for b > 0 {
		if b%2 == 1 {
			total*=tmp
		}
		b/=2
		tmp*=tmp
	}
	return total
}


func main() {
	// Scan開始前にSplitを設定しないとPanicになるため、ここに移動
	// 元のコードがnextLineを使用しているため、ScanLinesを設定します
	s.Split(bufio.ScanLines)

	N := nextLine()
	M := nextLine()

	n := N[len(N) - 1] - '0'
	m := M[len(M) - 1] - '0'
	if len(M) > 1 {
		v := M[len(M) - 2] - '0'
		m+=10*v
	}
	m%=4
	if m == 0 && M != "0" {
		m = 4
	}

	fmt.Println(pow(int(n), int(m))%10)


}
0