結果

問題 No.1595 The Final Digit
ユーザー ComiComi
提出日時 2021-07-10 20:18:28
言語 Go
(1.22.1)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,321 bytes
コンパイル時間 13,850 ms
コンパイル使用メモリ 203,144 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-14 19:52:57
合計ジャッジ時間 15,008 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,356 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,384 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

var reader = bufio.NewReader(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

func getIdentityMatrix(n int) [][]int {
	c := make([][]int, n)
	for i := 0; i < n; i++ {
		c[i] = make([]int, n)
		c[i][i] = 1
	}

	return c
}

func prodModMatrix(a, b [][]int, mod int) [][]int {
	aRowN := len(a)
	bColN := len(b[0])
	bRowN := len(b)

	c := make([][]int, aRowN)
	for i := 0; i < aRowN; i++ {
		c[i] = make([]int, bColN)
	}

	for i := 0; i < aRowN; i++ {
		for k := 0; k < bRowN; k++ {
			for j := 0; j < bColN; j++ {
				c[i][j] = (c[i][j] + a[i][k]*b[k][j]) % mod
			}
		}
	}

	return c
}

func powModMatrix(a [][]int, x int, mod int) [][]int {
	n := len(a)

	r := getIdentityMatrix(n)

	for x > 0 {
		if x&1 == 1 {
			r = prodModMatrix(r, a, mod)
		}
		a = prodModMatrix(a, a, mod)
		x >>= 1
	}
	return r
}
func toDigits(x, base int) []int {
	if x == 0 {
		return []int{0}
	}

	ans := make([]int, 0)
	for x != 0 {
		ans = append(ans, x%base)
		x = x / base
	}
	return ans
}

func main() {
	defer writer.Flush()

	var p, q, r, k int
	fmt.Fscan(reader, &p, &q, &r, &k)

	a := [][]int{
		{1, 1, 1},
		{1, 0, 0},
		{0, 1, 0},
	}

	b := powModMatrix(a, k-3, 10)
	p %= 10
	q %= 10
	r %= 10

	c := prodModMatrix(b, [][]int{{r}, {q}, {p}}, 10)

	fmt.Fprintf(writer, "%v", c[0][0])
}
0