結果

問題 No.1428 PeRmutation Question
ユーザー 箱星箱星
提出日時 2021-03-12 20:10:24
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 664 ms / 2,000 ms
コード長 3,814 bytes
コンパイル時間 13,887 ms
コンパイル使用メモリ 460,724 KB
実行使用メモリ 138,604 KB
最終ジャッジ日時 2024-10-14 10:22:35
合計ジャッジ時間 34,875 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 424 ms
138,428 KB
testcase_01 AC 418 ms
138,372 KB
testcase_02 AC 420 ms
138,540 KB
testcase_03 AC 421 ms
138,452 KB
testcase_04 AC 432 ms
138,604 KB
testcase_05 AC 416 ms
138,340 KB
testcase_06 AC 420 ms
138,472 KB
testcase_07 AC 431 ms
138,436 KB
testcase_08 AC 433 ms
138,492 KB
testcase_09 AC 424 ms
138,508 KB
testcase_10 AC 422 ms
138,296 KB
testcase_11 AC 430 ms
138,476 KB
testcase_12 AC 545 ms
137,608 KB
testcase_13 AC 629 ms
134,236 KB
testcase_14 AC 664 ms
134,448 KB
testcase_15 AC 592 ms
133,916 KB
testcase_16 AC 645 ms
130,720 KB
testcase_17 AC 663 ms
130,240 KB
testcase_18 AC 574 ms
135,052 KB
testcase_19 AC 573 ms
136,100 KB
testcase_20 AC 574 ms
136,172 KB
testcase_21 AC 636 ms
134,256 KB
testcase_22 AC 636 ms
133,740 KB
testcase_23 AC 610 ms
134,144 KB
testcase_24 AC 636 ms
134,164 KB
testcase_25 AC 622 ms
134,352 KB
testcase_26 AC 613 ms
134,780 KB
testcase_27 AC 628 ms
130,112 KB
testcase_28 AC 609 ms
135,576 KB
testcase_29 AC 662 ms
134,052 KB
testcase_30 AC 653 ms
134,196 KB
testcase_31 AC 620 ms
133,468 KB
testcase_32 AC 660 ms
134,196 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

// region ModInt
class ModInt(x: Long) {
    companion object {
        const val MOD = 1000000007L
        //const val MOD = 998244353L
    }

    constructor(y: Int) : this(y.toLong())

    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"
    }
}

fun Long.toModInt(): ModInt {
    return ModInt(this)
}

fun Int.toModInt(): ModInt {
    return ModInt(this)
}

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

class CombinationCalculator {
    companion object {
        private const val fac_size = 600000
    }

    private val arrFac = Array(fac_size) { ModInt(1) }
    private val arrInvfac = Array(fac_size) { ModInt(1) }

    init {
        for (i in 1 until fac_size) {
            arrFac[i] = arrFac[i - 1] * ModInt(i)
        }
        arrInvfac[fac_size - 1] = arrFac[fac_size - 1].inv()
        for (i in fac_size - 2 downTo 1) {
            arrInvfac[i] = arrInvfac[i + 1] * ModInt(i + 1)
        }
    }

    fun fac(n: Int): ModInt {
        return arrFac[n]
    }

    fun fac(n: Long): ModInt {
        return fac(n.toInt())
    }

    fun invfac(n: Int): ModInt {
        return arrInvfac[n]
    }

    fun invfac(n: Long): ModInt {
        return invfac(n.toInt())
    }

    fun binom(n: Long, k: Long): ModInt {
        return if (k in 0..n) {
            arrFac[n.toInt()] * arrInvfac[k.toInt()] * arrInvfac[(n - k).toInt()]
        } else {
            ModInt(0)
        }
    }
}

fun PrintWriter.solve() {
    val n = nextInt()
    val p = Array(n) { nextInt() - 1 }
    val map = mutableMapOf<Int, Int>()
    val cc = CombinationCalculator()
    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 half = true
    var ans = cc.fac(n)
    for ((k, v) in map) {
        if (k % 2 == 0 || v >= 2) half = false
        ans /= ModInt(k).pow(v.toLong())
        ans *= cc.invfac(v)
    }
    if (half && n != 1) ans /= ModInt(2)
    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