結果

問題 No.8108 徐々におかしくなる数学クイズ
コンテスト
ユーザー scaler
提出日時 2024-08-31 19:34:01
言語 Scala(Beta)
(3.8.1)
コンパイル:
scalac _filename_
実行:
java -cp .:/home/linuxbrew/.linuxbrew/Cellar/scala/3.8.1/libexec/maven2/org/scala-lang/scala3-library_3/3.8.1/scala3-library_3-3.8.1.jar:/home/linuxbrew/.linuxbrew/Cellar/scala/3.8.1/libexec/maven2/org/scala-lang/scala-library/3.8.1/scala-library-3.8.1.jar _class_
結果
AC  
実行時間 322 ms / 2,000 ms
コード長 490 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 9,872 ms
コンパイル使用メモリ 266,332 KB
実行使用メモリ 54,864 KB
最終ジャッジ日時 2026-03-09 20:12:25
合計ジャッジ時間 13,820 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 10
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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