結果

問題 No.1631 Sorting Integers (Multiple of K) Easy
ユーザー 👑 箱星箱星
提出日時 2021-07-30 21:31:32
言語 Kotlin
(1.9.23)
結果
TLE  
実行時間 -
コード長 2,547 bytes
コンパイル時間 15,250 ms
コンパイル使用メモリ 426,212 KB
実行使用メモリ 50,808 KB
最終ジャッジ日時 2023-10-14 03:38:43
合計ジャッジ時間 26,122 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 276 ms
50,044 KB
testcase_01 AC 273 ms
49,876 KB
testcase_02 AC 271 ms
50,048 KB
testcase_03 AC 314 ms
50,208 KB
testcase_04 AC 274 ms
49,780 KB
testcase_05 AC 274 ms
49,972 KB
testcase_06 AC 273 ms
49,876 KB
testcase_07 AC 273 ms
49,968 KB
testcase_08 AC 273 ms
50,576 KB
testcase_09 AC 280 ms
49,888 KB
testcase_10 AC 282 ms
49,996 KB
testcase_11 AC 279 ms
50,032 KB
testcase_12 AC 278 ms
50,308 KB
testcase_13 AC 281 ms
50,808 KB
testcase_14 TLE -
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 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

fun PrintWriter.solve() {
    val n = nextInt()
    val k = nextInt()
    val c = IntArray(9) { nextInt() }
    var ans = 0L
    val pow10 = IntArray(n) { 1 }
    for (i in 0 until n - 1) {
        pow10[i + 1] = pow10[i] * 10 % k
    }
    fun dfs(r: Int, step: Int, c1: Int, c2: Int, c3: Int, c4: Int, c5: Int, c6: Int, c7: Int, c8: Int, c9: Int) {
        if (step == n) {
            if (r == 0) ans++
        } else {
            if (c1 > 0) {
                val r2 = (r + pow10[step] * 1) % k
                dfs(r2, step + 1, c1 - 1, c2, c3, c4, c5, c6, c7, c8, c9)
            }
            if (c2 > 0) {
                val r2 = (r + pow10[step] * 2) % k
                dfs(r2, step + 1, c1, c2 - 1, c3, c4, c5, c6, c7, c8, c9)
            }
            if (c3 > 0) {
                val r2 = (r + pow10[step] * 3) % k
                dfs(r2, step + 1, c1, c2, c3 - 1, c4, c5, c6, c7, c8, c9)
            }
            if (c4 > 0) {
                val r2 = (r + pow10[step] * 4) % k
                dfs(r2, step + 1, c1, c2, c3, c4 - 1, c5, c6, c7, c8, c9)
            }
            if (c5 > 0) {
                val r2 = (r + pow10[step] * 5) % k
                dfs(r2, step + 1, c1, c2, c3, c4, c5 - 1, c6, c7, c8, c9)
            }
            if (c6 > 0) {
                val r2 = (r + pow10[step] * 6) % k
                dfs(r2, step + 1, c1, c2, c3, c4, c5, c6 - 1, c7, c8, c9)
            }
            if (c7 > 0) {
                val r2 = (r + pow10[step] * 7) % k
                dfs(r2, step + 1, c1, c2, c3, c4, c5, c6, c7 - 1, c8, c9)
            }
            if (c8 > 0) {
                val r2 = (r + pow10[step] * 8) % k
                dfs(r2, step + 1, c1, c2, c3, c4, c5, c6, c7, c8 - 1, c9)
            }
            if (c9 > 0) {
                val r2 = (r + pow10[step] * 9) % k
                dfs(r2, step + 1, c1, c2, c3, c4, c5, c6, c7, c8, c9 - 1)
            }
        }
    }
    dfs(0, 0, c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7], c[8])
    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