結果

問題 No.3108 徐々におかしくなる数学クイズ
ユーザー scalerscaler
提出日時 2024-08-31 19:34:01
言語 Scala(Beta)
(3.4.0)
結果
AC  
実行時間 892 ms / 2,000 ms
コード長 490 bytes
コンパイル時間 11,224 ms
コンパイル使用メモリ 262,292 KB
実行使用メモリ 64,300 KB
最終ジャッジ日時 2024-08-31 19:34:23
合計ジャッジ時間 20,959 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 892 ms
64,212 KB
testcase_01 AC 860 ms
64,000 KB
testcase_02 AC 862 ms
64,204 KB
testcase_03 AC 880 ms
64,300 KB
testcase_04 AC 884 ms
64,284 KB
testcase_05 AC 867 ms
64,244 KB
testcase_06 AC 855 ms
64,196 KB
testcase_07 AC 892 ms
64,192 KB
testcase_08 AC 863 ms
64,168 KB
testcase_09 AC 869 ms
64,048 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import scala.io.StdIn.readLine

@main
def yuki3108(): Unit =
  val n = readLine.toLong
  println(fib(n))

def fib(n: Long, mod: Long = 998244353L): Long =
  def fastFibo(i: Long): (Long, Long) = i match {
    case 0 => (0, 1)
    case 1 => (1, 1)
    case _ =>
      val (a, b) = fastFibo(i / 2)
      if i % 2 == 0 then
        (a * (b * 2 + mod - a) % mod, (a * a + b * b) % mod)
      else
        ((a * a + b * b) % mod, (b * (2 * a + b)) % mod)
  }
  val (x, _) = fastFibo(n)
  x % mod
0