結果

問題 No.1747 Many Formulae 2
コンテスト
ユーザー yudedako
提出日時 2022-01-25 00:28:02
言語 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  
実行時間 389 ms / 2,000 ms
コード長 837 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 11,054 ms
コンパイル使用メモリ 267,628 KB
実行使用メモリ 58,356 KB
最終ジャッジ日時 2026-03-09 19:26:37
合計ジャッジ時間 19,704 ms
ジャッジサーバーID
(参考情報)
judge2_1 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import scala.annotation.tailrec
import scala.io.StdIn.*


def isPrime(value: Long): Boolean =
  @tailrec def isPrimeInner(p: Long): Boolean =
    if p * p > value then
      true
    else
      value % p != 0 && isPrimeInner(p + 1)
  if value <= 2 then
    value == 2
  else
    isPrimeInner(2)

def countExpression(digits: List[Int])(predicate: Long => Boolean): Int =
  def enumerate(rest: List[Int], confirmed: Long = 0, last: Long = 0): Int =
    rest match
      case Nil => if predicate(confirmed + last) then 1 else 0
      case h::Nil => enumerate(Nil, confirmed, last * 10 + h)
      case h::t => enumerate(t, confirmed, last * 10 + h) + enumerate(t, confirmed + last * 10 + h, 0)
  enumerate(digits)

@main def main =
  val expr = readLine().map(_.asDigit).toList
  val result = countExpression(expr){isPrime}
  println(result)
0