結果

問題 No.634 硬貨の枚数1
ユーザー GoryudyumaGoryudyuma
提出日時 2018-06-11 15:09:45
言語 Scala(Beta)
(3.4.0)
結果
AC  
実行時間 1,224 ms / 2,000 ms
コード長 4,128 bytes
コンパイル時間 12,642 ms
コンパイル使用メモリ 261,068 KB
実行使用メモリ 63,596 KB
最終ジャッジ日時 2023-09-13 03:03:37
合計ジャッジ時間 101,304 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,012 ms
62,856 KB
testcase_01 AC 966 ms
62,668 KB
testcase_02 AC 997 ms
62,732 KB
testcase_03 AC 1,012 ms
63,120 KB
testcase_04 AC 1,037 ms
62,876 KB
testcase_05 AC 1,076 ms
63,108 KB
testcase_06 AC 1,047 ms
63,068 KB
testcase_07 AC 1,030 ms
63,016 KB
testcase_08 AC 1,029 ms
63,156 KB
testcase_09 AC 1,027 ms
62,888 KB
testcase_10 AC 1,033 ms
62,948 KB
testcase_11 AC 1,037 ms
63,060 KB
testcase_12 AC 1,023 ms
62,816 KB
testcase_13 AC 1,024 ms
62,876 KB
testcase_14 AC 1,067 ms
63,188 KB
testcase_15 AC 1,157 ms
63,144 KB
testcase_16 AC 1,106 ms
62,836 KB
testcase_17 AC 1,128 ms
62,816 KB
testcase_18 AC 1,138 ms
62,824 KB
testcase_19 AC 1,044 ms
62,824 KB
testcase_20 AC 1,065 ms
62,800 KB
testcase_21 AC 1,186 ms
62,744 KB
testcase_22 AC 1,175 ms
62,940 KB
testcase_23 AC 1,106 ms
62,784 KB
testcase_24 AC 1,037 ms
62,776 KB
testcase_25 AC 1,038 ms
62,844 KB
testcase_26 AC 1,036 ms
62,872 KB
testcase_27 AC 1,034 ms
63,056 KB
testcase_28 AC 1,027 ms
62,872 KB
testcase_29 AC 1,011 ms
63,320 KB
testcase_30 AC 1,033 ms
62,832 KB
testcase_31 AC 1,034 ms
63,140 KB
testcase_32 AC 1,044 ms
62,784 KB
testcase_33 AC 1,029 ms
62,852 KB
testcase_34 AC 1,062 ms
62,912 KB
testcase_35 AC 1,099 ms
63,272 KB
testcase_36 AC 1,144 ms
62,840 KB
testcase_37 AC 1,129 ms
62,916 KB
testcase_38 AC 1,093 ms
62,896 KB
testcase_39 AC 1,169 ms
62,988 KB
testcase_40 AC 1,136 ms
62,796 KB
testcase_41 AC 1,149 ms
62,960 KB
testcase_42 AC 1,166 ms
62,752 KB
testcase_43 AC 1,114 ms
62,892 KB
testcase_44 AC 1,036 ms
63,032 KB
testcase_45 AC 1,050 ms
62,800 KB
testcase_46 AC 1,100 ms
62,804 KB
testcase_47 AC 1,080 ms
62,788 KB
testcase_48 AC 949 ms
62,692 KB
testcase_49 AC 1,041 ms
62,864 KB
testcase_50 AC 1,182 ms
63,140 KB
testcase_51 AC 1,090 ms
63,136 KB
testcase_52 AC 1,014 ms
62,864 KB
testcase_53 AC 1,021 ms
62,876 KB
testcase_54 AC 1,030 ms
63,136 KB
testcase_55 AC 1,180 ms
62,860 KB
testcase_56 AC 1,181 ms
62,920 KB
testcase_57 AC 1,224 ms
63,020 KB
testcase_58 AC 1,196 ms
63,060 KB
testcase_59 AC 1,202 ms
62,896 KB
testcase_60 AC 1,194 ms
63,104 KB
testcase_61 AC 1,162 ms
62,820 KB
testcase_62 AC 1,198 ms
63,148 KB
testcase_63 AC 1,194 ms
62,896 KB
testcase_64 AC 1,187 ms
63,164 KB
testcase_65 AC 1,180 ms
62,908 KB
testcase_66 AC 1,185 ms
63,596 KB
testcase_67 AC 1,180 ms
62,840 KB
testcase_68 AC 1,198 ms
63,152 KB
testcase_69 AC 1,184 ms
63,068 KB
testcase_70 AC 1,174 ms
63,192 KB
testcase_71 AC 1,185 ms
62,844 KB
testcase_72 AC 1,205 ms
62,912 KB
testcase_73 AC 1,178 ms
63,072 KB
testcase_74 AC 1,182 ms
62,880 KB
testcase_75 AC 1,181 ms
62,912 KB
testcase_76 AC 1,027 ms
62,780 KB
testcase_77 AC 959 ms
62,992 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