結果

問題 No.634 硬貨の枚数1
ユーザー GoryudyumaGoryudyuma
提出日時 2018-06-11 15:09:45
言語 Scala(Beta)
(3.4.0)
結果
AC  
実行時間 1,153 ms / 2,000 ms
コード長 4,128 bytes
コンパイル時間 12,118 ms
コンパイル使用メモリ 278,044 KB
実行使用メモリ 65,200 KB
最終ジャッジ日時 2024-06-30 13:37:48
合計ジャッジ時間 96,931 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 957 ms
63,576 KB
testcase_01 AC 899 ms
63,400 KB
testcase_02 AC 992 ms
63,600 KB
testcase_03 AC 999 ms
63,872 KB
testcase_04 AC 1,008 ms
63,492 KB
testcase_05 AC 1,013 ms
63,736 KB
testcase_06 AC 988 ms
63,824 KB
testcase_07 AC 971 ms
63,744 KB
testcase_08 AC 983 ms
63,480 KB
testcase_09 AC 976 ms
63,636 KB
testcase_10 AC 979 ms
63,724 KB
testcase_11 AC 981 ms
63,656 KB
testcase_12 AC 980 ms
63,500 KB
testcase_13 AC 1,001 ms
63,584 KB
testcase_14 AC 1,016 ms
63,544 KB
testcase_15 AC 1,093 ms
63,756 KB
testcase_16 AC 1,052 ms
63,584 KB
testcase_17 AC 1,074 ms
63,848 KB
testcase_18 AC 1,079 ms
63,800 KB
testcase_19 AC 1,006 ms
63,668 KB
testcase_20 AC 1,021 ms
63,644 KB
testcase_21 AC 1,116 ms
63,556 KB
testcase_22 AC 1,103 ms
63,664 KB
testcase_23 AC 1,056 ms
63,776 KB
testcase_24 AC 977 ms
63,488 KB
testcase_25 AC 962 ms
63,764 KB
testcase_26 AC 998 ms
63,744 KB
testcase_27 AC 1,008 ms
63,680 KB
testcase_28 AC 1,021 ms
63,888 KB
testcase_29 AC 1,005 ms
63,752 KB
testcase_30 AC 1,005 ms
63,860 KB
testcase_31 AC 1,030 ms
63,648 KB
testcase_32 AC 1,022 ms
63,540 KB
testcase_33 AC 1,021 ms
63,664 KB
testcase_34 AC 1,036 ms
63,840 KB
testcase_35 AC 1,058 ms
63,564 KB
testcase_36 AC 1,090 ms
63,692 KB
testcase_37 AC 1,104 ms
63,588 KB
testcase_38 AC 1,028 ms
63,524 KB
testcase_39 AC 1,122 ms
63,804 KB
testcase_40 AC 1,086 ms
63,668 KB
testcase_41 AC 1,080 ms
63,740 KB
testcase_42 AC 1,097 ms
63,912 KB
testcase_43 AC 1,045 ms
63,704 KB
testcase_44 AC 979 ms
63,504 KB
testcase_45 AC 1,036 ms
63,676 KB
testcase_46 AC 973 ms
63,792 KB
testcase_47 AC 1,084 ms
65,200 KB
testcase_48 AC 955 ms
63,280 KB
testcase_49 AC 975 ms
63,752 KB
testcase_50 AC 1,102 ms
63,596 KB
testcase_51 AC 1,032 ms
63,720 KB
testcase_52 AC 956 ms
63,584 KB
testcase_53 AC 964 ms
63,552 KB
testcase_54 AC 976 ms
63,480 KB
testcase_55 AC 1,096 ms
63,744 KB
testcase_56 AC 1,110 ms
63,860 KB
testcase_57 AC 1,104 ms
63,708 KB
testcase_58 AC 1,075 ms
63,744 KB
testcase_59 AC 1,090 ms
63,732 KB
testcase_60 AC 1,115 ms
63,836 KB
testcase_61 AC 1,134 ms
63,696 KB
testcase_62 AC 1,120 ms
63,592 KB
testcase_63 AC 1,091 ms
63,688 KB
testcase_64 AC 1,136 ms
63,748 KB
testcase_65 AC 1,114 ms
63,812 KB
testcase_66 AC 1,132 ms
63,516 KB
testcase_67 AC 1,135 ms
63,744 KB
testcase_68 AC 1,153 ms
63,524 KB
testcase_69 AC 1,119 ms
63,728 KB
testcase_70 AC 1,097 ms
63,636 KB
testcase_71 AC 1,116 ms
63,744 KB
testcase_72 AC 1,109 ms
63,668 KB
testcase_73 AC 1,092 ms
63,388 KB
testcase_74 AC 1,079 ms
63,476 KB
testcase_75 AC 1,080 ms
63,656 KB
testcase_76 AC 966 ms
63,564 KB
testcase_77 AC 920 ms
63,364 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner

import scala.collection.Searching._
import scala.annotation.tailrec
import scala.collection.mutable
import scala.collection.immutable._
import scala.io.StdIn.readLine

class IUnionFind(val size: Int) {

  private case class Node(var parent: Option[Int], var treeSize: Int)

  private val nodes = Array.fill[Node](size)(new Node(None, 1))

  def union(t1: Int, t2: Int): IUnionFind = {
    if (t1 == t2) return this

    val root1 = root(t1)
    val root2 = root(t2)
    if (root1 == root2) return this

    val node1 = nodes(root1)
    val node2 = nodes(root2)

    if (node1.treeSize < node2.treeSize) {
      node1.parent = Some(t2)
      node2.treeSize += node1.treeSize
    } else {
      node2.parent = Some(t1)
      node1.treeSize += node2.treeSize
    }
    this
  }

  def connected(t1: Int, t2: Int): Boolean = t1 == t2 || root(t1) == root(t2)

  def root(t: Int): Int = nodes(t).parent match {
    case None => t
    case Some(p) => root(p)
  }
}

object Main {
  def solve(sc: => Scanner): Unit = {
    val N = sc.nextLong
    val sankaku = Util.getPermutation().take(10000).map(i => i * (i + 1) / 2)
    println(if (sankaku.contains(N)) (1) else if (calc(sankaku, N)) (2) else (3))
  }

  def calc(s: Seq[Long], N: Long): Boolean = {
    if (s.head > N) (false) else if (s.contains(N - s.head)) (true) else (calc(s.tail, N))
  }

  def recursive(N: Long, now: Long): Long = {
    if (N == 0) (0) else {
      val price = now * (now + 1) / 2
      if (N >= price) {
        println(price)
        recursive(N - price, now) + 1
      } else (recursive(N, now - 1))
    }
  }

  def recursive2(X: Long, prime: Long): (Long, Long) = {
    if (X % prime != 0) ((1, X)) else {
      if (X % (prime * prime) == 0) (recursive2(X / prime / prime, prime)) else ((prime, X / prime))
    }
  }

  def check(A: Array[List[Int]]): Boolean = {
    A.map(_.size == 0).fold(true)((a, b) => a & b)
  }

  def shift(n: Long): Long = {
    if (n == 0) (0) else if (n == 1) (1) else (shift(n - 1) << 1)
  }

  def unShift(n: Long): Long = {
    if (n == 0) (0) else (unShift(n >> 1) + 1)
  }

  def gcd(i: Long, j: Long): Long = {
    if (i < j) (gcd(j, i)) else (if (j == 0) (i) else (gcd(j, i % j)))
  }

  def main(args: Array[String]): Unit = {
    val sc: Scanner = new Scanner(System.in)
    solve(sc)
  }
}

object Util {
  def getPermutation(begin: Long = 0): Stream[Long] =
    Stream.cons(begin, getPermutation(begin + 1))

  def getPrimeList(): Stream[Long] =
    getPrimeListRecursive(getPermutation(begin = 2))

  private def getPrimeListRecursive(A: Stream[Long]): Stream[Long] =
    Stream.cons(A.head, getPrimeListRecursive(A.tail.filter(_ % A.head != 0)))

  def fib(a: Long = 0, b: Long = 1, mod: Long = Long.MaxValue): Stream[Long] = a #:: fib(b % mod, (a + b) % mod, mod)
}

object ArabicRoman {

  type =?>[A, B] = PartialFunction[A, B]

  val codeTable = List(
    (1000, "M"), (900, "CM"), (500, "D"), (400, "CD"), (100, "C"),
    (90, "XC"), (50, "L"), (40, "XL"), (10, "X"), (9, "IX"), (5, "V"), (4, "IV"), (1, "I"))

  val arabicToRoman: (Int) =?> String = {
    case src if (src >= 1 && src <= 3999) => {

      def convert(left: Int, cont: String = "", code: List[(Int, String)] = codeTable): String = {
        val (unitVal, unitChar) = code.head
        left - unitVal match {
          case n if (n == 0) => cont + unitChar
          case n if (n > 0) => convert(n, cont + unitChar, code)
          case _ => convert(left, cont, code.tail)
        }
      }

      convert(src)
    }
  }

  val romanToArabic: (String) =?> Int = {
    case src if (Option(src).exists { s => {
      s.nonEmpty && ("""[^MDCLXVI]""".r.findFirstMatchIn(s.toUpperCase) == None)
    }
    }) => {

      def convert(left: String, cont: Int = 0, code: List[(Int, String)] = codeTable): Int = {
        val (unitVal, unitChar) = code.head
        left.splitAt(unitChar.length) match {
          case ("", _) => cont
          case (`unitChar`, tail) => convert(tail, cont + unitVal, code)
          case _ => convert(left, cont, code.tail)
        }
      }

      convert(src.toUpperCase())
    }
  }
}
0