結果
問題 | No.573 a^2[i] = a[i] |
ユーザー |
![]() |
提出日時 | 2024-05-10 04:00:53 |
言語 | Kotlin (2.1.0) |
結果 |
AC
|
実行時間 | 389 ms / 2,000 ms |
コード長 | 1,114 bytes |
コンパイル時間 | 13,861 ms |
コンパイル使用メモリ | 434,208 KB |
実行使用メモリ | 62,036 KB |
最終ジャッジ日時 | 2024-12-18 01:04:01 |
合計ジャッジ時間 | 31,613 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 47 |
ソースコード
fun main() {val mod = 1_000_000_007Lval n = readLine()!!.toInt()val fact = LongArray(200001) { 1L }for (i in 1 until fact.size) {fact[i] = fact[i - 1] * i % mod}val factInv = LongArray(fact.size) { i ->if (i == fact.lastIndex) fact.last().modPow(mod - 2, mod)else 0L}for (i in fact.lastIndex - 1 downTo 0) {factInv[i] = factInv[i + 1] * (i + 1) % mod}fun combi(a: Int, b: Int): Long {return if (0 <= b && b <= a) {fact[a] * factInv[b] % mod * factInv[a - b] % mod} else {0L}}var ans = 0Lfor (i in 1..n) {ans = (ans + combi(n, i) * i.toLong().modPow((n - i).toLong(), mod) % mod) % mod}println(ans)}private fun Long.modPow(exponent: Long, modulus: Long): Long {var result = 1Lvar exp = exponentvar base = this % moduluswhile (exp > 0) {if (exp and 1 == 1L) {result = (result * base) % modulus}base = (base * base) % modulusexp = exp shr 1}return result}