結果

問題 No.1330 Multiply or Divide
ユーザー 箱星箱星
提出日時 2021-01-08 22:22:39
言語 Kotlin
(2.1.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,510 bytes
コンパイル時間 16,778 ms
コンパイル使用メモリ 454,388 KB
実行使用メモリ 353,308 KB
最終ジャッジ日時 2024-11-16 18:39:48
合計ジャッジ時間 42,632 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 45 TLE * 1
権限があれば一括ダウンロードができます
コンパイルメッセージ
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