結果

問題 No.101 ぐるぐる!あみだくじ!
ユーザー tshigtshig
提出日時 2016-08-19 12:22:29
言語 Ruby
(3.3.0)
結果
AC  
実行時間 90 ms / 5,000 ms
コード長 815 bytes
コンパイル時間 248 ms
コンパイル使用メモリ 11,248 KB
実行使用メモリ 15,484 KB
最終ジャッジ日時 2023-08-07 14:18:19
合計ジャッジ時間 5,453 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
15,148 KB
testcase_01 AC 80 ms
15,236 KB
testcase_02 AC 81 ms
15,268 KB
testcase_03 AC 80 ms
15,284 KB
testcase_04 AC 87 ms
15,160 KB
testcase_05 AC 82 ms
15,480 KB
testcase_06 AC 81 ms
15,280 KB
testcase_07 AC 82 ms
15,088 KB
testcase_08 AC 81 ms
15,228 KB
testcase_09 AC 81 ms
15,304 KB
testcase_10 AC 81 ms
15,040 KB
testcase_11 AC 83 ms
15,232 KB
testcase_12 AC 86 ms
15,112 KB
testcase_13 AC 86 ms
15,484 KB
testcase_14 AC 85 ms
15,160 KB
testcase_15 AC 85 ms
15,040 KB
testcase_16 AC 88 ms
15,288 KB
testcase_17 AC 87 ms
15,196 KB
testcase_18 AC 84 ms
15,268 KB
testcase_19 AC 86 ms
15,260 KB
testcase_20 AC 85 ms
15,276 KB
testcase_21 AC 88 ms
15,044 KB
testcase_22 AC 85 ms
15,176 KB
testcase_23 AC 87 ms
15,312 KB
testcase_24 AC 83 ms
15,156 KB
testcase_25 AC 88 ms
15,280 KB
testcase_26 AC 88 ms
15,336 KB
testcase_27 AC 89 ms
15,348 KB
testcase_28 AC 83 ms
15,080 KB
testcase_29 AC 88 ms
15,292 KB
testcase_30 AC 86 ms
15,064 KB
testcase_31 AC 87 ms
15,212 KB
testcase_32 AC 86 ms
15,148 KB
testcase_33 AC 90 ms
15,360 KB
testcase_34 AC 78 ms
15,300 KB
testcase_35 AC 77 ms
15,144 KB
testcase_36 AC 77 ms
15,112 KB
testcase_37 AC 79 ms
15,020 KB
testcase_38 AC 80 ms
15,300 KB
testcase_39 AC 81 ms
15,176 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

class Calc0101
  def initialize(args)
    args = args.map { |l| l.chomp.split(/\s+/) }
    @n = args.shift.first.to_i
    @k = args.shift.first.to_i
    @xys = args.map { |l| l.map(&:to_i) }
  end

  def run
    pattern = calc_pattern
    cycles = (1..@n).map { |k| calc_cycle(pattern, k) }
    cycles.inject { |a, b| a * b / a.gcd(b) }
  end

  def calc_cycle(pattern, k)
    c = 0
    s = k
    begin
      c += 1
      k = pattern[k - 1]
    end until s == k
    c
  end

  def calc_pattern
    (1..@n).map { |k| calc_amida(k) }
  end

  def calc_amida(k)
    i = -1
    loop do
      j = @xys.drop(i + 1).index { |x, y| x == k || y == k }
      break unless j
      i += j + 1
      k = @xys[i][0] == k ? @xys[i][1] : @xys[i][0]
    end
    k
  end
end

puts Calc0101.new(STDIN.readlines).run if __FILE__ == $0
0