結果

問題 No.303 割れません
ユーザー Min_25Min_25
提出日時 2015-11-15 19:27:13
言語 PyPy2
(7.3.15)
結果
AC  
実行時間 639 ms / 10,000 ms
コード長 3,252 bytes
コンパイル時間 1,941 ms
コンパイル使用メモリ 76,804 KB
実行使用メモリ 98,836 KB
最終ジャッジ日時 2024-09-13 15:11:28
合計ジャッジ時間 9,975 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
75,328 KB
testcase_01 AC 73 ms
75,556 KB
testcase_02 AC 72 ms
75,536 KB
testcase_03 AC 273 ms
84,232 KB
testcase_04 AC 599 ms
97,696 KB
testcase_05 AC 602 ms
98,836 KB
testcase_06 AC 595 ms
97,804 KB
testcase_07 AC 614 ms
96,668 KB
testcase_08 AC 636 ms
98,492 KB
testcase_09 AC 634 ms
98,828 KB
testcase_10 AC 71 ms
75,268 KB
testcase_11 AC 594 ms
96,952 KB
testcase_12 AC 639 ms
98,192 KB
testcase_13 AC 370 ms
88,860 KB
testcase_14 AC 211 ms
81,428 KB
testcase_15 AC 158 ms
78,708 KB
testcase_16 AC 73 ms
75,552 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 print_big_integer(N, out=None, newline=True, beg=0, tens=None):
  def rec(N, e, zerofill=False):
    if e <= 10:
      if not zerofill:
        out.write("%s" % str(N)[beg:])
      else:
        out.write("%0*d" % (1 << e, N))
    else:
      ten = tens[e-1]
      q, r = fast_divmod(N, ten)
      if zerofill or q:
        rec(q, e - 1, zerofill)
        rec(r, e - 1, True)
      else:
        rec(r, e - 1, zerofill)

  if out is None:
    import sys
    out = sys.stdout

  if N < 0:
    N = -N
    out.write("-")

  if tens is None:
    tens = []
    ten = 10
    while ten <= N:
      tens.append(ten)
      if ten.bit_length() * 2 - 1 > N.bit_length():
        break
      ten *= ten

  e = 0
  while e < len(tens) and N >= tens[e]:
    e += 1

  rec(N, e)

  if newline:
    out.write("\n")

  return

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(ans)
    print_big_integer(ans)

prob303()
0