結果

問題 No.254 文字列の構成
ユーザー くわいくわい
提出日時 2015-09-26 21:28:26
言語 Scala(Beta)
(3.4.0)
結果
AC  
実行時間 965 ms / 5,000 ms
コード長 1,435 bytes
コンパイル時間 10,500 ms
コンパイル使用メモリ 262,312 KB
実行使用メモリ 62,952 KB
最終ジャッジ日時 2023-08-12 18:42:16
合計ジャッジ時間 43,669 ms
ジャッジサーバーID
(参考情報)
judge10 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 946 ms
62,440 KB
testcase_01 AC 940 ms
62,444 KB
testcase_02 AC 945 ms
62,664 KB
testcase_03 AC 947 ms
62,616 KB
testcase_04 AC 947 ms
62,504 KB
testcase_05 AC 942 ms
62,424 KB
testcase_06 AC 948 ms
62,564 KB
testcase_07 AC 951 ms
62,460 KB
testcase_08 AC 947 ms
62,516 KB
testcase_09 AC 959 ms
62,560 KB
testcase_10 AC 943 ms
62,620 KB
testcase_11 AC 932 ms
62,392 KB
testcase_12 AC 964 ms
62,416 KB
testcase_13 AC 948 ms
62,360 KB
testcase_14 AC 949 ms
62,748 KB
testcase_15 AC 940 ms
62,408 KB
testcase_16 AC 949 ms
62,632 KB
testcase_17 AC 951 ms
62,512 KB
testcase_18 AC 953 ms
62,420 KB
testcase_19 AC 953 ms
62,692 KB
testcase_20 AC 942 ms
62,420 KB
testcase_21 AC 950 ms
62,436 KB
testcase_22 AC 941 ms
62,952 KB
testcase_23 AC 947 ms
62,592 KB
testcase_24 AC 958 ms
62,576 KB
testcase_25 AC 954 ms
62,560 KB
testcase_26 AC 956 ms
62,648 KB
testcase_27 AC 952 ms
62,632 KB
testcase_28 AC 941 ms
62,620 KB
testcase_29 AC 946 ms
62,852 KB
testcase_30 AC 945 ms
62,340 KB
testcase_31 AC 965 ms
62,536 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner

object Problem254 {

  // 好きな寿司ネタは寒ブリ
  def main(args: Array[String]) = {
    val sc = new Scanner(System.in)
    val n = sc.nextInt

    val sb = new StringBuilder
    var remain = n
    var charIndex = 0
    while (remain > 0) {
      val len = binarySearch(ABPalindrome, remain, 1, 65000)
      remain -= ABPalindrome(len)

      val subPalindrome = getABString(len, charIndex)
      sb.append(subPalindrome)
      charIndex += 2
    }
    println(sb.toString)
  }

  /*
  len以下の(偶数 or 奇数)の和
  6 abab 4 2
  9 ababa 5 3 1
  12 ababab 6 4 2
  16 abababa 7 5 3 1
  20 abababab 8 6 4 2
  25 ababababa 9 7 5 3 1
  */
  def ABPalindrome(n: Int): Int = {
    n % 2 match {
      case 0 => (n / 2) * (n / 2 + 1)
      case 1 => Math.pow((n / 2 + 1), 2).toInt
    }
  }

  def binarySearch(f: Int => Int, n: Int, low: Int, high: Int): Int = {
    val mid = (low + high) / 2

    if (low > high) {
      return mid
    }

    (f(mid), n) match {
      case (x, y) if x == y => mid
      case (x, y) if x < y => binarySearch(f, n, mid + 1, high)
      case _ => binarySearch(f, n, low, mid - 1)
    }
  }

  def getABString(len: Int, charIndex: Int): String = {
    val a = ('a' + charIndex).toChar
    val b = ('b' + charIndex).toChar
    val ab = a.toString + b.toString

    len % 2 match {
      case 0 => ab * (len / 2)
      case 1 => ab * (len / 2) + a
    }
  }

}
0