結果

問題 No.2045 Two Reflections
ユーザー 箱星箱星
提出日時 2022-03-09 03:28:56
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 418 ms / 2,000 ms
コード長 5,784 bytes
コンパイル時間 16,227 ms
コンパイル使用メモリ 461,076 KB
実行使用メモリ 68,780 KB
最終ジャッジ日時 2024-09-20 03:00:50
合計ジャッジ時間 27,679 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 298 ms
52,420 KB
testcase_01 AC 299 ms
52,328 KB
testcase_02 AC 380 ms
60,728 KB
testcase_03 AC 407 ms
59,896 KB
testcase_04 AC 392 ms
60,768 KB
testcase_05 AC 337 ms
54,240 KB
testcase_06 AC 350 ms
55,632 KB
testcase_07 AC 393 ms
60,396 KB
testcase_08 AC 373 ms
61,484 KB
testcase_09 AC 399 ms
62,468 KB
testcase_10 AC 418 ms
60,124 KB
testcase_11 AC 341 ms
54,408 KB
testcase_12 AC 269 ms
50,104 KB
testcase_13 AC 269 ms
49,872 KB
testcase_14 AC 273 ms
50,104 KB
testcase_15 AC 267 ms
49,968 KB
testcase_16 AC 294 ms
52,368 KB
testcase_17 AC 261 ms
49,944 KB
testcase_18 AC 265 ms
50,144 KB
testcase_19 AC 270 ms
49,996 KB
testcase_20 AC 418 ms
68,780 KB
testcase_21 AC 380 ms
60,868 KB
testcase_22 AC 377 ms
60,920 KB
testcase_23 AC 371 ms
60,864 KB
testcase_24 AC 382 ms
60,896 KB
testcase_25 AC 377 ms
61,124 KB
testcase_26 AC 381 ms
60,864 KB
testcase_27 AC 375 ms
60,924 KB
testcase_28 AC 380 ms
60,848 KB
testcase_29 AC 377 ms
60,952 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

// 置換の積を計算
fun prod(p: Array<Int>, q: Array<Int>): Array<Int> {
    val n = p.size
    val r = Array(n) { 0 }
    for (i in 0 until n) {
        r[i] = q[p[i]]
    }
    return r
}

fun makeReflectionLeft(n: Int, p: Int): Array<Int> {
    val x = Array(n) { it }
    for (i in 0 until p) {
        x[p - 1 - i] = i
    }
    return x
}

fun makeReflectionRight(n: Int, q: Int): Array<Int> {
    val x = Array(n) { it }
    for (i in n - q until n) {
        x[i] = 2 * n - q - i - 1
    }
    return x
}

fun PrintWriter.solve() {
    val n = nextInt()
    val p = nextInt()
    val q = nextInt()
    if (p == 1 && q == 1) {
        println(1)
    } else if (p == 1 || q == 1) {
        println(2)
    } else {
        val x = makeReflectionLeft(n, p)
        val y = makeReflectionRight(n, q)
        val xy = prod(x, y)
        val uf = UnionFind(n)
        for (i in 0 until n) {
            uf.unite(i, xy[i])
        }
        val set = mutableSetOf<Int>()
        for (i in 0 until n) {
            set.add(uf.find(i))
        }
        val sizes = mutableSetOf<Int>()
        for (i in set) {
            sizes.add(uf.size(i))
        }
        val minfac = Array(100005) { it }
        for (d in 2 until 100005) {
            if (minfac[d] != d) continue
            var i = 2
            while (d * i < 100005) {
                minfac[d * i] = min(minfac[d * i], d)
                i++
            }
        }
        val lcm = mutableMapOf<Int, Int>()
        for (size in sizes) {
            val pf = mutableMapOf<Int, Int>()
            var m = size
            while (m > 1) {
                val fac = minfac[m]
                pf[fac] = (pf[fac] ?: 0) + 1
                m /= fac
            }
            for ((fac, e) in pf) {
                lcm[fac] = max(lcm[fac] ?: 0, e)
            }
        }
        var ans = ModInt(2)
        for ((fac, e) in lcm) {
            ans *= ModInt(fac).pow(e.toLong())
        }
        println(ans)
    }
}

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

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

class ModIntArray(n: Int) {
    private val a = LongArray(n) { 0L }

    constructor(n: Int, init: (Int) -> ModInt) : this(n) {
        for (i in 0 until n) {
            a[i] = init(i).x
        }
    }

    operator fun set(i: Int, x: ModInt) {
        a[i] = x.x
    }

    operator fun get(i: Int): ModInt {
        return ModInt(a[i])
    }

    fun joinToString(s: String): String {
        return a.joinToString(s)
    }
}

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 UnionFind(n: Int) {
    private val rank = IntArray(n) { 0 }
    private val parent = IntArray(n) { -1 }

    fun find(x: Int): Int {
        if (parent[x] < 0) {
            return x
        }
        parent[x] = find(parent[x])
        return parent[x]
    }

    fun unite(x: Int, y: Int) {
        val x1 = find(x)
        val y1 = find(y)
        if (x1 == y1) {
            return
        }
        if (rank[x1] < rank[y1]) {
            parent[y1] += parent[x1]
            parent[x1] = y1
        } else {
            parent[x1] += parent[y1]
            parent[y1] = x1
            if (rank[x1] == rank[y1]) {
                rank[x1]++
            }
        }
    }

    fun same(x: Int, y: Int): Boolean {
        return find(x) == find(y)
    }

    fun size(x: Int): Int {
        return -parent[find(x)]
    }
}

fun main() {
    Thread(null, {
        val writer = PrintWriter(System.out, false)
        writer.solve()
        writer.flush()
    }, "solve", abs(1L.shl(26)))
        .apply { setUncaughtExceptionHandler { _, e -> e.printStackTrace(); kotlin.system.exitProcess(1) } }
        .apply { start() }.join()
}

// 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