結果

問題 No.1595 The Final Digit
ユーザー NatsubiSoganNatsubiSogan
提出日時 2021-07-11 19:00:29
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 19 ms / 2,000 ms
コード長 3,620 bytes
コンパイル時間 128 ms
コンパイル使用メモリ 11,112 KB
実行使用メモリ 8,780 KB
最終ジャッジ日時 2023-09-14 20:17:43
合計ジャッジ時間 1,383 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,672 KB
testcase_01 AC 18 ms
8,704 KB
testcase_02 AC 17 ms
8,756 KB
testcase_03 AC 17 ms
8,628 KB
testcase_04 AC 18 ms
8,748 KB
testcase_05 AC 17 ms
8,656 KB
testcase_06 AC 19 ms
8,656 KB
testcase_07 AC 18 ms
8,712 KB
testcase_08 AC 17 ms
8,780 KB
testcase_09 AC 17 ms
8,596 KB
testcase_10 AC 17 ms
8,432 KB
testcase_11 AC 17 ms
8,596 KB
testcase_12 AC 17 ms
8,680 KB
testcase_13 AC 17 ms
8,604 KB
testcase_14 AC 18 ms
8,668 KB
testcase_15 AC 18 ms
8,632 KB
testcase_16 AC 19 ms
8,652 KB
testcase_17 AC 17 ms
8,756 KB
testcase_18 AC 17 ms
8,756 KB
testcase_19 AC 18 ms
8,704 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 10

#拡張Euclidの互除法
def extgcd(a, b, d = 0):
    g = a
    if b == 0:
        x, y = 1, 0
    else:
        x, y, g = extgcd(b, a % b)
        x, y = y, x - a // b * y
    return x, y, g
 
#mod p における逆元
def invmod(a, p):
    x, y, g = extgcd(a, p)
    x %= p
    return x

#行列ライブラリ(遅い)
class Matrix:
    def __init__(self, n, m, mat=None):
        self.n = n
        self.m = m
        self.mat = [[0] * self.m for i in range(self.n)]
        if mat:
            for i in range(self.n):
                self.mat[i] = mat[i]
    
    def is_square(self):
        return self.n == self.m
    
    def __getitem__(self, key):
        if isinstance(key, slice):
            return self.mat[key]
        else:
            assert key >= 0
            return self.mat[key]

    def id(n):
        res = Matrix(n, n)
        for i in range(n):
            res[i][i] = 1
        return res

    def __len__(self):
        return len(self.mat)
    
    def __str__(self):
        return "\n".join(" ".join(map(str, self[i])) for i in range(self.n))

    def times(self, k):
        res = [[0] * self.m for i in range(self.n)]
        for i in range(self.n):
            for j in range(self.m):
                res[i][j] = k * self[i][j] % mod
        return Matrix(self.n, self.m, res)

    def __pos__(self):
        return self

    def __neg__(self):
        return self.times(-1)

    def __add__(self, other):
        res = [[0] * self.m for i in range(self.n)]
        for i in range(self.n):
            for j in range(self.m):
                res[i][j] = (self[i][j] + other[i][j]) % mod
        return Matrix(self.n, self.m, res)
    
    def __sub__(self, other):
        res = [[0] * self.m for i in range(self.n)]
        for i in range(self.n):
            for j in range(self.m):
                res[i][j] = (self[i][j] - other[i][j]) % mod
        return Matrix(self.n, self.m, res)

    def __mul__(self, other):
        if other.__class__ == Matrix:
            res = [[0] * other.m for i in range(self.n)]
            for i in range(self.n):
                for k in range(self.m):
                    for j in range(other.m):
                        res[i][j] += self[i][k] * other[k][j]
                        res[i][j] %= mod
            return Matrix(self.n, other.m, res)
        else:
            return self.times(other)
    
    def __rmul__(self, other):
        return self.times(other)

    def __pow__(self, k):
        tmp = Matrix(self.n, self.n, self.mat)
        res = Matrix.id(self.n)
        while k:
            if k & 1:
                res *= tmp
            tmp *= tmp
            k >>= 1
        return res

    def determinant(self):
        res = 1
        tmp  = Matrix(self.n, self.n, self.mat)
        for j in range(self.n):
            if tmp[j][j] == 0:
                for i in range(j + 1, self.n):
                    if tmp[i][j] != 0: break
                else:
                    return 0
                tmp.mat[j], tmp.mat[i] = tmp.mat[i], tmp.mat[j]
                res *= -1
            inv = invmod(tmp[j][j], mod)
            for i in range(j + 1, self.n):
                c = -inv * tmp[i][j] % mod
                for k in range(self.n):
                    tmp[i][k] += c * tmp[j][k]
                    tmp[i][k] %= mod
        for i in range(self.n):
            res *= tmp[i][i]
            res %= mod
        return res

p, q, r, k = map(int, input().split())
p %= 10; q %= 10; r %= 10
b, m = Matrix(3, 1, [[r], [q], [p]]), Matrix(3, 3, [[1, 1, 1], [1, 0, 0], [0, 1, 0]])
m **= k - 3
ans = m * b
print(ans[0][0])
0