結果

問題 No.303 割れません
ユーザー Min_25Min_25
提出日時 2015-11-16 00:13:15
言語 PyPy2
(7.3.15)
結果
AC  
実行時間 558 ms / 10,000 ms
コード長 3,241 bytes
コンパイル時間 679 ms
コンパイル使用メモリ 76,928 KB
実行使用メモリ 99,200 KB
最終ジャッジ日時 2024-10-14 17:13:05
合計ジャッジ時間 7,307 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
75,264 KB
testcase_01 AC 80 ms
75,264 KB
testcase_02 AC 79 ms
75,264 KB
testcase_03 AC 237 ms
82,432 KB
testcase_04 AC 533 ms
97,024 KB
testcase_05 AC 531 ms
99,200 KB
testcase_06 AC 527 ms
97,920 KB
testcase_07 AC 543 ms
96,512 KB
testcase_08 AC 557 ms
97,024 KB
testcase_09 AC 558 ms
96,896 KB
testcase_10 AC 82 ms
75,520 KB
testcase_11 AC 519 ms
96,256 KB
testcase_12 AC 558 ms
97,024 KB
testcase_13 AC 356 ms
87,424 KB
testcase_14 AC 223 ms
80,512 KB
testcase_15 AC 156 ms
78,592 KB
testcase_16 AC 81 ms
75,648 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def fib(n):
  if n <= 2:
    return ((n + 1) >> 1, (n + 2) >> 1)

  a, b = fib((n >> 1) - 1)

  # a * a is slightly faster than a * b in PyPy
  # (rpython/rlib/rbigint.py)

  a2, b2 = a * a, b * b
  e = (-2 if (n >> 1) & 1 else 2)

  c =  b2       + a2
  d = (b2 << 2) - a2 + e
  if n & 1:
    return (d, (d << 1) - c)
  else:
    return (d - c, d)

def fast_divmod(a, b):
  """
  Burnikel & Ziegler
  """

  DIVMOD_THRESHOLD = 6000

  def _pack(pack, shamt):
    size = len(pack)
    while size > 1:
      npack = []
      for i in range(0, size - 1, 2):
        npack.append(pack[i] | (pack[i+1] << shamt))
      if size & 1:
        npack.append(pack[-1])
      pack = npack
      size = (size + 1) >> 1
      shamt <<= 1
    return pack[0]

  def _unpack(M, size, shamt):
    needed_sizes = []
    s = size
    while s > 1:
      needed_sizes.append(s)
      s = (s + 1) >> 1
    ret = [M]
    for needed_size in needed_sizes[::-1]:
      mask = (1 << shamt) - 1
      nret = []
      for c in ret:
        nret.append(c & mask)
        nret.append(c >> shamt)
      ret = nret[:needed_size]
      shamt >>= 1
    return ret

  def _div32(a21, a0, b, b1, b0, n):
    if (a21 >> n) >= b1:
      q, r = (1 << n) - 1, a21 - (b1 << n) + b1
    else:
      q, r = _div21(a21, b1, n)
    r = ((r << n) | a0) - q * b0
    while r < 0:
      q -= 1
      r += b
    return q, r

  def _div21(a, b, n):
    if a < b:
      return (0, a)
    if n <= DIVMOD_THRESHOLD:
      return divmod(a, b)
    odd = n & 1
    if odd:
      a, b, n = a << 1, b << 1, n + 1
    nh = n >> 1
    mask = (1 << nh) - 1
    b1, b0 = b >> nh, b & mask
    q1, r = _div32(a >> n, (a >> nh) & mask, b, b1, b0, nh)
    q0, r = _div32(r, a & mask, b, b1, b0, nh)
    if odd:
      r >>= 1
    return (q1 << nh) | q0, r

  if a < b:
    return (0, a)

  n = b.bit_length()

  if n <= DIVMOD_THRESHOLD:
    return divmod(a, b)

  nqs = a.bit_length() // n
  a_blocks = _unpack(a, nqs + 1, (n << (nqs.bit_length() - 1)))

  qs = []
  r = a_blocks[-1]
  for i in range(nqs - 1, -1, -1):
    q, r = _div21((r << n) | a_blocks[i], b, n)
    qs.append(q)
  q = _pack(qs[::-1], n)
  return q, r

def fast_str(N):
  def rec(N, e, zerofill=False):
    if e <= 10:
      if not zerofill:
        strs.append(str(N))
      else:
        strs.append("%0*d" % (1 << (e + 1), N))
    else:
      shamt = 1 << e
      q, r = fast_divmod(N >> shamt, fives[e])
      r = (r << shamt) | (N & masks[e])
      if zerofill or q:
        rec(q, e - 1, zerofill)
        rec(r, e - 1, True)
      else:
        rec(r, e - 1, zerofill)

  strs = []

  if N < 0:
    N = -N
    strs.append("-")

  fives = []
  masks = []

  mask = 1
  five = 5
  e = 0

  while 1:
    shamt = 1 << e
    if (five << shamt) > N:
      break
    fives.append(five)
    masks.append(mask)
    if (five.bit_length() + shamt) * 2 - 1 > N.bit_length():
      break

    e += 1
    five *= five
    mask |= mask << shamt

  rec(N, e)
  return "".join(strs)

def prob303():
  import sys
  n = int(sys.stdin.readline())
  if n == 2:
    print("3")
    print("INF")
  else:
    if n & 1:
      ans = fib(n)[0]
    else:
      a, b = fib(n // 2 - 1)
      ans = (a * b) << 1
    print(n)
    print(fast_str(ans))

prob303()
0