結果

問題 No.1747 Many Formulae 2
ユーザー yudedakoyudedako
提出日時 2022-01-25 00:28:02
言語 Scala(Beta)
(3.4.0)
結果
AC  
実行時間 952 ms / 2,000 ms
コード長 837 bytes
コンパイル時間 12,392 ms
コンパイル使用メモリ 251,356 KB
実行使用メモリ 62,128 KB
最終ジャッジ日時 2023-08-21 15:49:12
合計ジャッジ時間 32,788 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 934 ms
61,904 KB
testcase_01 AC 938 ms
61,924 KB
testcase_02 AC 938 ms
62,048 KB
testcase_03 AC 934 ms
61,880 KB
testcase_04 AC 936 ms
61,924 KB
testcase_05 AC 936 ms
61,796 KB
testcase_06 AC 927 ms
62,024 KB
testcase_07 AC 932 ms
61,964 KB
testcase_08 AC 937 ms
62,000 KB
testcase_09 AC 937 ms
61,876 KB
testcase_10 AC 930 ms
61,940 KB
testcase_11 AC 948 ms
61,908 KB
testcase_12 AC 943 ms
62,128 KB
testcase_13 AC 933 ms
62,008 KB
testcase_14 AC 942 ms
61,692 KB
testcase_15 AC 952 ms
61,944 KB
testcase_16 AC 952 ms
61,880 KB
testcase_17 AC 950 ms
61,956 KB
testcase_18 AC 941 ms
61,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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