結果

問題 No.1428 PeRmutation Question
ユーザー 箱星箱星
提出日時 2020-12-07 01:13:13
言語 Kotlin
(1.9.23)
結果
WA  
実行時間 -
コード長 3,228 bytes
コンパイル時間 15,826 ms
コンパイル使用メモリ 462,156 KB
実行使用メモリ 91,612 KB
最終ジャッジ日時 2024-10-01 19:44:14
合計ジャッジ時間 34,242 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 304 ms
56,780 KB
testcase_01 AC 300 ms
56,764 KB
testcase_02 AC 301 ms
56,828 KB
testcase_03 AC 302 ms
56,760 KB
testcase_04 AC 301 ms
56,796 KB
testcase_05 WA -
testcase_06 AC 311 ms
56,816 KB
testcase_07 AC 300 ms
56,868 KB
testcase_08 AC 295 ms
56,804 KB
testcase_09 AC 303 ms
56,812 KB
testcase_10 WA -
testcase_11 AC 295 ms
56,884 KB
testcase_12 AC 431 ms
77,844 KB
testcase_13 AC 633 ms
91,052 KB
testcase_14 AC 588 ms
91,160 KB
testcase_15 AC 567 ms
88,936 KB
testcase_16 AC 648 ms
91,204 KB
testcase_17 AC 624 ms
91,240 KB
testcase_18 AC 514 ms
88,728 KB
testcase_19 AC 486 ms
88,984 KB
testcase_20 AC 490 ms
88,764 KB
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 WA -
testcase_30 AC 638 ms
90,904 KB
testcase_31 WA -
testcase_32 AC 647 ms
91,048 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader
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 {
    return fact(n, true) * fact(k, false) * fact(n - k, false)
}

fun comb2(n: Long, k: Long): ModInt {
    var ans = ModInt(1)
    for (i in 0 until k) {
        ans = ans * ModInt(n - i) / ModInt(k - i)
    }
    return ans
}
// endregion

fun PrintWriter.solve(sc: FastScanner) {
    val n = sc.nextInt()
    val p = Array(n) { sc.nextInt() - 1 }
    val map = mutableMapOf<Int, Int>()
    val visited = Array(n) { false }
    for (i in 0 until n) {
        if (!visited[i]) {
            var l = 0
            var v = i
            while (!visited[v]) {
                visited[v] = true
                v = p[v]
                l++
            }
            map[l] = (map[l] ?: 0) + 1
        }
    }
    var ans = fact(n.toLong(), true)
    for ((k, v) in map) {
        ans /= ModInt(k.toLong()).pow(v.toLong())
        ans *= fact(v.toLong(), false)
    }
    println(ans)
}

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

class FastScanner(s: InputStream) {
    private var st = StringTokenizer("")
    private val br = BufferedReader(InputStreamReader(s))

    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()
    fun ready() = br.ready()
}
0