All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g1101_1200.s1175_prime_arrangements.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
package g1101_1200.s1175_prime_arrangements

// #Easy #Math #2023_05_25_Time_129_ms_(50.00%)_Space_33.7_MB_(50.00%)

@Suppress("NAME_SHADOWING")
class Solution {
    fun numPrimeArrangements(n: Int): Int {
        var n = n
        val a = intArrayOf(
            2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83,
            89, 97
        )
        var c = 0
        while (c < 25 && n >= a[c]) {
            c++
        }
        val m = 1000000007
        var res = 1L
        while (n - c > 0) {
            res *= (n - c).toLong()
            res %= m.toLong()
            n--
        }
        while (c > 0) {
            res *= c.toLong()
            res %= m.toLong()
            c--
        }
        return res.toInt()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy