結果

問題 No.1330 Multiply or Divide
ユーザー 👑 箱
提出日時 2021-01-08 22:22:39
言語 Kotlin
(1.9.10)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,510 bytes
コンパイル時間 14,842 ms
コンパイル使用メモリ 426,724 KB
実行使用メモリ 261,808 KB
最終ジャッジ日時 2023-08-10 12:39:18
合計ジャッジ時間 19,905 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 294 ms
57,036 KB
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:18:24: warning: unnecessary non-null assertion (!!) on a non-null receiver of type Long
    val max_a = a.max()!!
                       ^
Main.kt:19:24: warning: unnecessary non-null assertion (!!) on a non-null receiver of type Long
    val max_b = b.max()!!
                       ^

ソースコード

diff #

import java.io.PrintWriter
import java.util.*
import kotlin.math.*

fun PrintWriter.solve() {
    val n = nextInt()
    val m = nextLong()
    val p = nextLong()
    val a = Array(n) { nextLong() }
    val b = a.copyOf()
    val c = Array(n) { 0 }
    for (i in 0 until n) {
        while (b[i] % p == 0L) {
            b[i] /= p
            c[i]++
        }
    }
    val max_a = a.max()!!
    val max_b = b.max()!!
    if (max_b == 1L && max_a < m) {
        println(-1)
        return
    }
    val stack = Stack<Long>()
    stack.push(1)
    val map = mutableMapOf<Long, Int>()
    map[1] = 0
    var ans = Int.MAX_VALUE
    while (stack.isNotEmpty()) {
        val now = stack.pop()
        if (now * max_a > m) {
            ans = min(ans, map[now]!! + 1)
        } else {
            for (i in 0 until n) {
                val nxt = now * b[i]
                if (!map.containsKey(nxt)) stack.push(nxt)
                map[nxt] = min(map[nxt] ?: Int.MAX_VALUE, map[now]!! + c[i] + 1)
            }
        }
    }
    println(ans)
}

fun main() {
    val writer = PrintWriter(System.out, false)
    writer.solve()
    writer.flush()
}

// region Scanner
private var st = StringTokenizer("")
private val br = System.`in`.bufferedReader()

fun next(): String {
    while (!st.hasMoreTokens()) st = StringTokenizer(br.readLine())
    return st.nextToken()
}

fun nextInt() = next().toInt()
fun nextLong() = next().toLong()
fun nextLine() = br.readLine()!!
fun nextDouble() = next().toDouble()
// endregion
0