結果

問題 No.1340 おーじ君をさがせ
ユーザー maguroflymagurofly
提出日時 2021-01-19 23:27:59
言語 Ruby
(3.3.0)
結果
AC  
実行時間 166 ms / 2,000 ms
コード長 1,201 bytes
コンパイル時間 299 ms
コンパイル使用メモリ 11,484 KB
実行使用メモリ 27,032 KB
最終ジャッジ日時 2023-08-22 19:05:27
合計ジャッジ時間 8,947 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
15,060 KB
testcase_01 AC 79 ms
15,336 KB
testcase_02 AC 76 ms
15,148 KB
testcase_03 AC 77 ms
15,176 KB
testcase_04 AC 76 ms
15,060 KB
testcase_05 AC 76 ms
15,156 KB
testcase_06 AC 78 ms
15,088 KB
testcase_07 AC 76 ms
15,180 KB
testcase_08 AC 75 ms
15,296 KB
testcase_09 AC 76 ms
15,276 KB
testcase_10 AC 79 ms
15,176 KB
testcase_11 AC 93 ms
19,576 KB
testcase_12 AC 84 ms
16,636 KB
testcase_13 AC 87 ms
18,756 KB
testcase_14 AC 104 ms
23,656 KB
testcase_15 AC 83 ms
16,504 KB
testcase_16 AC 84 ms
16,440 KB
testcase_17 AC 81 ms
16,056 KB
testcase_18 AC 88 ms
18,884 KB
testcase_19 AC 80 ms
15,276 KB
testcase_20 AC 83 ms
15,300 KB
testcase_21 AC 111 ms
18,308 KB
testcase_22 AC 127 ms
22,512 KB
testcase_23 AC 113 ms
18,396 KB
testcase_24 AC 157 ms
26,644 KB
testcase_25 AC 95 ms
15,680 KB
testcase_26 AC 88 ms
15,440 KB
testcase_27 AC 87 ms
15,428 KB
testcase_28 AC 96 ms
15,756 KB
testcase_29 AC 85 ms
15,424 KB
testcase_30 AC 112 ms
18,740 KB
testcase_31 AC 166 ms
27,032 KB
testcase_32 AC 147 ms
24,244 KB
testcase_33 AC 148 ms
24,724 KB
testcase_34 AC 148 ms
24,836 KB
testcase_35 AC 156 ms
25,336 KB
testcase_36 AC 78 ms
15,140 KB
testcase_37 AC 91 ms
15,192 KB
testcase_38 AC 100 ms
16,112 KB
testcase_39 AC 120 ms
26,056 KB
testcase_40 AC 121 ms
26,040 KB
testcase_41 AC 121 ms
26,068 KB
testcase_42 AC 76 ms
15,172 KB
testcase_43 AC 77 ms
15,048 KB
testcase_44 AC 77 ms
15,308 KB
testcase_45 AC 76 ms
15,176 KB
testcase_46 AC 111 ms
25,832 KB
testcase_47 AC 110 ms
25,832 KB
testcase_48 AC 113 ms
26,516 KB
testcase_49 AC 108 ms
25,820 KB
testcase_50 AC 107 ms
25,796 KB
testcase_51 AC 108 ms
25,708 KB
testcase_52 AC 119 ms
25,380 KB
testcase_53 AC 117 ms
25,172 KB
testcase_54 AC 115 ms
25,400 KB
testcase_55 AC 116 ms
26,456 KB
testcase_56 AC 76 ms
15,292 KB
testcase_57 AC 77 ms
15,348 KB
testcase_58 AC 76 ms
15,260 KB
testcase_59 AC 103 ms
25,328 KB
testcase_60 AC 77 ms
15,260 KB
testcase_61 AC 102 ms
25,168 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

class BitAndOrMatrix
  attr_reader :n, :bits
  def initialize(n, bits)
    @n, @bits = n, bits
    @u = (1 << n) - 1
    @v = ((@u + 1)**n - 1) / @u
  end

  def self.rows(rows)
    n, bits = rows.size, 0
    rows.each_with_index do |row, i|
      x = 0
      row.each_with_index do |a, j|
        x |= a << j
      end
      bits |= x << n * i
    end
    new(n, bits)
  end

  def self.id(n)
    new(n, ((1 << (n + 1) * n) - 1) / ((1 << n + 1) - 1))
  end

  def [](i, j)
    @bits >> i * @n + j & 1
  end

  def +(other)
    self.class.new(@n, @bits | other.bits)
  end

  def *(other)
    a, b = @bits, other.bits
    bits = 0
    while a > 0 and b > 0
      bits |= ((a & @v) * @u) & ((b & @u) * @v)
      a >>= 1
      b >>= n
    end
    self.class.new(@n, bits)
  end

  def **(e)
    r = self.class.id(@n)
    x = self
    while e > 0
      r *= x if (e & 1) == 1
      x *= x
      e >>= 1
    end
    r
  end

  def to_a
    (0 ... @n).map { |i| (0 ... @n).map { |j| self[i, j] } }
  end
end

N, M, T = gets.split.map(&:to_i)
rows = Array.new(N) { [0] * N }
M.times do
  a, b = gets.split.map(&:to_i)
  rows[b][a] = 1
end

ans = BitAndOrMatrix.rows(rows) ** T
puts ans.to_a.transpose[0].sum
0