結果

問題 No.1917 LCMST
ユーザー yakamotoyakamoto
提出日時 2022-04-29 23:07:38
言語 Kotlin
(1.9.23)
結果
WA  
実行時間 -
コード長 4,405 bytes
コンパイル時間 16,776 ms
コンパイル使用メモリ 444,624 KB
実行使用メモリ 168,464 KB
最終ジャッジ日時 2023-09-11 14:39:06
合計ジャッジ時間 90,022 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 547 ms
75,736 KB
testcase_01 AC 549 ms
75,764 KB
testcase_02 AC 544 ms
75,652 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 538 ms
75,764 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 1,545 ms
165,940 KB
testcase_30 AC 1,542 ms
165,900 KB
testcase_31 AC 1,559 ms
165,968 KB
testcase_32 AC 1,569 ms
165,824 KB
testcase_33 AC 1,558 ms
166,164 KB
testcase_34 AC 1,120 ms
120,864 KB
testcase_35 AC 1,045 ms
120,924 KB
testcase_36 AC 1,180 ms
120,924 KB
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 TLE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:172:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debug(a: LongArray) {
          ^
Main.kt:176:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debug(a: IntArray) {
          ^
Main.kt:180:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debug(a: BooleanArray) {
          ^
Main.kt:184:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun toString(a: BooleanArray) = run{a.map { if (it) 1 else 0 }.joinToString("")}
          ^
Main.kt:186:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debugDim(A: Array<LongArray>) {
          ^
Main.kt:193:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debugDim(A: Array<IntArray>) {
          ^
Main.kt:200:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debugDim(A: Array<BooleanArray>) {
          ^
Main.kt:217:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun assert(b: Boolean) = run{if (!b) throw AssertionError()}
          ^

ソースコード

diff #

import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader
import java.lang.AssertionError
import java.util.*
import kotlin.math.abs
import kotlin.math.max
import kotlin.math.min

val MOD = 1_000_000_007

fun divisors(x: Int): MutableList<Int> {
  val res = mutableListOf<Int>()
  val post = mutableListOf<Int>()
  var d = 1
  while (d * d <= x) {
    if (x % d == 0) {
      res += d
      if (d * d != x) post += x / d
    }
    ++d
  }
  for (i in post.size - 1 downTo 0) {
    res += post[i]
  }
  return res
}
tailrec fun gcd(a: Int, b: Int): Int {
  if (b == 0) return abs(a)
  return gcd(b, a % b)
}
class Solver(stream: InputStream, private val out: java.io.PrintWriter) {
  private val reader = BufferedReader(InputStreamReader(stream), 32768)

  fun solve() {
    val N = ni()
    val A = na(N)
    val MAX = 100_000
    val D = Array(MAX + 1){divisors(it)}
    val C = IntArray(MAX + 1)
    for (a in A) {
      C[a]++
    }
    val bests = Array(MAX + 1){ mutableListOf<Int>() }

    for (d in 1 .. MAX) {
      var y = d
      while(y <= MAX) {
        if (C[y] > 0) bests[d].add(y)
        y += d
      }
    }

    fun calc(a: Int): Long {
      val divs = D[a]
      var best = 1e9.toInt()
      for (i in divs.indices) {
        val d = divs[i]
        if (bests[d].isEmpty()) continue
        val b =
          if (bests[d][0] != a || C[a] > 1) bests[d][0]
          else bests[d].getOrNull(1) ?: continue
        best = min(best, b / gcd(a, b))
      }

      debug{"clac($a)=$best"}

      return best.toLong() * a
    }

    val candidates = mutableListOf<Long>()
    for (a in 1 .. MAX) {
      for (i in 0 until C[a]) candidates += calc(a)
    }
    candidates.sort()
    debug{candidates.toString()}
    val ans = candidates.slice(1 until N).sum()

    out.println(ans)
  }













































  private val isDebug = try {
    // なんか本番でエラーでる
    System.getenv("MY_DEBUG") != null
  } catch (t: Throwable) {
    false
  }

  private var tokenizer: StringTokenizer? = null
  private fun next(): String {
    while (tokenizer == null || !tokenizer!!.hasMoreTokens()) {
      tokenizer = StringTokenizer(reader.readLine())
    }
    return tokenizer!!.nextToken()
  }

  private fun ni() = next().toInt()
  private fun nl() = next().toLong()
  private fun ns() = next()
  private fun na(n: Int, offset: Int = 0): IntArray {
    return IntArray(n) { ni() + offset }
  }
  private fun nal(n: Int, offset: Int = 0): LongArray {
    val res = LongArray(n)
    for (i in 0 until n) {
      res[i] = nl() + offset
    }
    return res
  }

  private fun na2(n: Int, offset: Int = 0): Array<IntArray> {
    val a  = Array(2){IntArray(n)}
    for (i in 0 until n) {
      for (e in a) {
        e[i] = ni() + offset
      }
    }
    return a
  }

  private inline fun debug(msg: () -> String) {
    if (isDebug) System.err.println(msg())
  }

  /**
   * コーナーケースでエラー出たりするので、debug(dp[1])のように添え字付きの場合はdebug{}をつかうこと
   */
  private inline fun debug(a: LongArray) {
    debug { a.joinToString(" ") }
  }

  private inline fun debug(a: IntArray) {
    debug { a.joinToString(" ") }
  }

  private inline fun debug(a: BooleanArray) {
    debug { toString(a) }
  }

  private inline fun toString(a: BooleanArray) = run{a.map { if (it) 1 else 0 }.joinToString("")}

  private inline fun debugDim(A: Array<LongArray>) {
    if (isDebug) {
      for (a in A) {
        debug(a)
      }
    }
  }
  private inline fun debugDim(A: Array<IntArray>) {
    if (isDebug) {
      for (a in A) {
        debug(a)
      }
    }
  }
  private inline fun debugDim(A: Array<BooleanArray>) {
    if (isDebug) {
      for (a in A) {
        debug(a)
      }
    }
  }

  /**
   * 勝手にimport消されるのを防ぎたい
   */
  private fun hoge() {
    min(1, 2)
    max(1, 2)
    abs(-10)
  }

  private inline fun assert(b: Boolean) = run{if (!b) throw AssertionError()}
  private inline fun assert(b: Boolean, f: () -> String) = run{if (!b) throw AssertionError(f())}

  companion object {
    // TestRunnerから呼びたいので単純なmainじゃだめ
    fun main() {
      val out = java.io.PrintWriter(System.out)
      Solver(System.`in`, out).solve()
      out.flush()
    }
  }
}

/**
 * judgeから呼ばれる
 */
fun main() = Solver.main()
0