結果

問題 No.1340 おーじ君をさがせ
ユーザー lam6er
提出日時 2025-04-16 16:18:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 116 ms / 2,000 ms
コード長 1,450 bytes
コンパイル時間 186 ms
コンパイル使用メモリ 81,404 KB
実行使用メモリ 77,080 KB
最終ジャッジ日時 2025-04-16 16:19:08
合計ジャッジ時間 5,041 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 59
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    import sys
    input = sys.stdin.read().split()
    idx = 0
    N = int(input[idx]); idx +=1
    M = int(input[idx]); idx +=1
    T = int(input[idx]); idx +=1
    
    edges = [[] for _ in range(N)]
    for _ in range(M):
        a = int(input[idx]); idx +=1
        b = int(input[idx]); idx +=1
        edges[a].append(b)
    
    # Build transition matrix
    transition = [0] * N
    for u in range(N):
        if len(edges[u]) > 0:
            mask = 0
            for v in edges[u]:
                mask |= 1 << v
            transition[u] = mask
        else:
            transition[u] = 0  # No outgoing edges
    
    def multiply(a, b):
        n = len(a)
        result = [0] * n
        for i in range(n):
            ai = a[i]
            j = 0
            while ai > 0 and j < n:
                if ai & 1:
                    result[i] |= b[j]
                ai >>= 1
                j += 1
        return result
    
    def matrix_power(mat, power):
        n = len(mat)
        result = [0] * n
        # Initialize as identity matrix
        for i in range(n):
            result[i] = 1 << i
        while power > 0:
            if power % 2 == 1:
                result = multiply(result, mat)
            mat = multiply(mat, mat)
            power //= 2
        return result
    
    powered = matrix_power(transition, T)
    ans = bin(powered[0]).count('1')
    print(ans)

if __name__ == '__main__':
    main()
0