結果

問題 No.1595 The Final Digit
ユーザー ComiComi
提出日時 2021-07-17 00:26:09
言語 Go
(1.22.1)
結果
WA  
実行時間 -
コード長 1,079 bytes
コンパイル時間 11,789 ms
コンパイル使用メモリ 211,408 KB
実行使用メモリ 12,036 KB
最終ジャッジ日時 2023-09-20 16:55:28
合計ジャッジ時間 12,009 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

package main

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

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

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 := make([]int, 1000000)
	a[1] = p % 10
	a[2] = q % 10
	a[3] = r % 10

	used := make([]int, 1009)
	used[(a[1] + a[2] + a[3]%10)] = 3

	m2 := 0
	for i := 4; i <= k; i++ {
		a[i] = (a[i-1] + a[i-2] + a[i-3]) % 10
		nex := (a[i]*100 + a[i-1]*10 + a[i-2]*1) % 10
		if used[nex] != 0 {
			m2 = i - used[nex]
			break
		}
		used[nex] = i
	}
	if m2 == 0 {
		fmt.Fprintf(writer, "%v", a[k])
		return
	}

	n2 := k % m2

	a = make([]int, 1000000)
	for i := 1; i <= n2; i++ {
		if i == 1 {
			a[i] = p % 10
			continue
		}
		if i == 2 {
			a[i] = q % 10
			continue
		}
		if i == 3 {
			a[i] = r % 10
			continue
		}

		a[i] = (a[i-1] + a[i-2] + a[i-3]) % 10
	}

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