結果

問題 No.1340 おーじ君をさがせ
ユーザー lam6er
提出日時 2025-04-15 23:26:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 131 ms / 2,000 ms
コード長 1,091 bytes
コンパイル時間 218 ms
コンパイル使用メモリ 82,168 KB
実行使用メモリ 76,328 KB
最終ジャッジ日時 2025-04-15 23:28:22
合計ジャッジ時間 5,507 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 59
権限があれば一括ダウンロードができます

ソースコード

diff #

N, M, T = map(int, input().split())
graph = [[] for _ in range(N)]
for _ in range(M):
    a, b = map(int, input().split())
    graph[a].append(b)

# Create the adjacency matrix using bitmasks
mat = [0] * N
for u in range(N):
    mask = 0
    for v in graph[u]:
        mask |= 1 << v
    mat[u] = mask

def multiply(A, B):
    n = len(A)
    result = [0] * n
    for i in range(n):
        a = A[i]
        res = 0
        j = 0
        while a > 0:
            if a & 1:
                res |= B[j]
            a >>= 1
            j += 1
        result[i] = res
    return result

def matrix_power(mat, power):
    n = len(mat)
    # Identity matrix where each row i has only the i-th bit set
    result = [1 << i for i in range(n)]
    while power > 0:
        if power % 2 == 1:
            result = multiply(result, mat)
        mat = multiply(mat, mat)
        power //= 2
    return result

# Compute the matrix raised to the T-th power
power_mat = matrix_power(mat, T)

# The answer is the number of set bits in the 0-th row of the resulting matrix
print(bin(power_mat[0]).count('1'))
0