結果

問題 No.2 素因数ゲーム
ユーザー vjudge1
提出日時 2025-09-04 09:47:15
言語 Crystal
(1.14.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,379 bytes
コンパイル時間 12,664 ms
コンパイル使用メモリ 318,064 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2025-09-04 09:47:30
合計ジャッジ時間 14,075 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

require "big"

primes = [] of BigInt
memo = [Hash(BigInt, Bool).new, Hash(BigInt, Bool).new]

def find_prime_factors(n : BigInt, primes : Array(BigInt))
  temp = n
  i = BigInt.new(2)
  while i * i <= temp
    if temp % i == 0
      primes << i
      while temp % i == 0
        temp //= i
      end
    end
    i += 1
  end
  primes << temp if temp > 1
end

def can_win(turn : Int32, num : BigInt, primes : Array(BigInt), memo : Array(Hash(BigInt, Bool))) : Bool
  if memo[turn].has_key?(num)
    return memo[turn][num]
  end
  
  if num == 1
    return turn == 1
  end
  
  if turn == 0
    primes.each do |p|
      if num % p != 0
        next
      end
      
      next_num = num
      while next_num % p == 0
        next_num //= p
        if can_win(1, next_num, primes, memo)
          memo[turn][num] = true
          return true
        end
      end
    end
    memo[turn][num] = false
    return false
  else
    primes.each do |p|
      if num % p != 0
        next
      end
      
      next_num = num
      while next_num % p == 0
        next_num //= p
        if !can_win(0, next_num, primes, memo)
          memo[turn][num] = false
          return false
        end
      end
    end
    memo[turn][num] = true
    return true
  end
end

n = BigInt.new(gets.not_nil!.chomp.to_s)
find_prime_factors(n, primes)
puts can_win(0, n, primes, memo) ? "Alice" : "Bob"
0