結果

問題 No.1202 お菓子の食べ方
ユーザー 箱星箱星
提出日時 2020-11-29 03:41:39
言語 Kotlin
(1.9.23)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,030 bytes
コンパイル時間 16,345 ms
コンパイル使用メモリ 454,324 KB
実行使用メモリ 173,992 KB
最終ジャッジ日時 2024-09-13 01:51:04
合計ジャッジ時間 78,246 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,531 ms
134,956 KB
testcase_01 AC 1,391 ms
118,444 KB
testcase_02 AC 1,837 ms
165,844 KB
testcase_03 AC 1,724 ms
149,452 KB
testcase_04 AC 1,637 ms
147,244 KB
testcase_05 AC 1,828 ms
165,752 KB
testcase_06 AC 1,733 ms
151,428 KB
testcase_07 AC 1,722 ms
149,188 KB
testcase_08 AC 1,659 ms
147,228 KB
testcase_09 AC 1,853 ms
153,368 KB
testcase_10 AC 682 ms
97,284 KB
testcase_11 AC 1,011 ms
114,592 KB
testcase_12 AC 1,255 ms
127,136 KB
testcase_13 AC 823 ms
111,796 KB
testcase_14 AC 1,456 ms
145,440 KB
testcase_15 AC 1,129 ms
136,344 KB
testcase_16 AC 1,486 ms
147,676 KB
testcase_17 AC 1,248 ms
139,364 KB
testcase_18 AC 1,002 ms
114,388 KB
testcase_19 AC 1,296 ms
144,632 KB
testcase_20 AC 1,004 ms
108,672 KB
testcase_21 AC 1,589 ms
159,996 KB
testcase_22 AC 1,484 ms
163,108 KB
testcase_23 AC 1,702 ms
166,220 KB
testcase_24 AC 1,686 ms
166,112 KB
testcase_25 AC 718 ms
99,584 KB
testcase_26 AC 1,576 ms
162,116 KB
testcase_27 AC 1,678 ms
166,100 KB
testcase_28 AC 1,022 ms
124,896 KB
testcase_29 AC 1,617 ms
163,948 KB
testcase_30 AC 297 ms
56,780 KB
testcase_31 AC 308 ms
56,816 KB
testcase_32 AC 300 ms
56,964 KB
testcase_33 AC 300 ms
56,784 KB
testcase_34 AC 301 ms
56,880 KB
testcase_35 AC 301 ms
56,848 KB
testcase_36 AC 298 ms
56,800 KB
testcase_37 AC 300 ms
56,864 KB
testcase_38 AC 310 ms
56,816 KB
testcase_39 AC 304 ms
56,888 KB
testcase_40 AC 1,994 ms
173,992 KB
testcase_41 AC 1,998 ms
173,980 KB
testcase_42 TLE -
testcase_43 AC 1,985 ms
173,852 KB
testcase_44 AC 1,981 ms
173,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.InputStream
import java.io.PrintWriter
import java.util.*

// ModInt
// region
class ModInt(x: Long) {

    companion object {
        const val MOD = 1000000007L
        //const val MOD = 998244353L
    }

    val x = (x % MOD + MOD) % MOD

    operator fun plus(other: ModInt): ModInt {
        return ModInt(x + other.x)
    }

    operator fun minus(other: ModInt): ModInt {
        return ModInt(x - other.x)
    }

    operator fun times(other: ModInt): ModInt {
        return ModInt(x * other.x)
    }

    operator fun div(other: ModInt): ModInt {
        return this * other.inv()
    }

    fun pow(exp: Long): ModInt {
        if (exp == 0L) return ModInt(1L)
        var a = pow(exp shr 1)
        a *= a
        if (exp and 1L == 0L) return a
        return this * a
    }

    fun inv(): ModInt {
        return this.pow(MOD - 2)
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as ModInt

        if (x != other.x) return false

        return true
    }

    override fun hashCode(): Int {
        return x.hashCode()
    }

    override fun toString(): String {
        return "$x"
    }

}

val fac = mutableListOf<ModInt>()
val invfac = mutableListOf<ModInt>()

fun fact(n: Long, b: Boolean): ModInt {
    if (fac.count() == 0) {
        fac.add(ModInt(1))
        invfac.add(ModInt(1))
    }
    while (fac.count() <= n) {
        fac.add(fac.last() * ModInt(fac.count().toLong()))
        invfac.add(fac.last().inv())
    }
    return if (b) {
        fac[n.toInt()]
    } else {
        invfac[n.toInt()]
    }
}

fun comb(n: Long, k: Long): ModInt {
    if (k !in 0..n) return ModInt(0)
    return fact(n, true) * fact(k, false) * fact(n - k, false)
}
// endregion

fun PrintWriter.solve() = with(FastScanner(System.`in`)) {
    val n = nextInt()
    val m = nextInt()
    val s = Array(n) { LongArray(m) { nextLong() } }
    var ans = ModInt(0)
    for (x in 0 until n) {
        for (y in 0 until m) {
            val v = s[x][y]
            ans += comb(x + y + v - 1, x.toLong()) * comb(y + v - 1, y.toLong())
            val v1 = if (y + 1 < m) s[x][y + 1] else 0
            ans += comb(x + y.toLong(), x.toLong()) * (comb(x + y + v, v - 1) - comb(x + y + v1, v1 - 1))
            val v2 = if (x + 1 < n) s[x + 1][y] else 0
            ans += comb(x + y.toLong(), x.toLong()) * (comb(x + y + v, v - 1) - comb(x + y + v2, v2 - 1))
        }
    }
    println(ans)
}

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

class FastScanner(s: InputStream) {
    private var st = StringTokenizer("")
    private val br = s.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()
}
0