結果

問題 No.1489 Repeat Cumulative Sum
ユーザー yakamotoyakamoto
提出日時 2021-04-23 22:59:53
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 473 ms / 2,000 ms
コード長 4,853 bytes
コンパイル時間 19,778 ms
コンパイル使用メモリ 442,104 KB
実行使用メモリ 59,580 KB
最終ジャッジ日時 2023-09-17 12:58:59
合計ジャッジ時間 34,064 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 288 ms
53,668 KB
testcase_01 AC 287 ms
53,824 KB
testcase_02 AC 289 ms
53,700 KB
testcase_03 AC 456 ms
59,580 KB
testcase_04 AC 456 ms
59,408 KB
testcase_05 AC 455 ms
59,248 KB
testcase_06 AC 461 ms
59,296 KB
testcase_07 AC 445 ms
57,208 KB
testcase_08 AC 439 ms
57,764 KB
testcase_09 AC 473 ms
59,552 KB
testcase_10 AC 460 ms
59,300 KB
testcase_11 AC 455 ms
57,368 KB
testcase_12 AC 312 ms
55,812 KB
testcase_13 AC 469 ms
57,348 KB
testcase_14 AC 471 ms
57,196 KB
testcase_15 AC 463 ms
59,412 KB
testcase_16 AC 440 ms
57,224 KB
testcase_17 AC 460 ms
57,192 KB
testcase_18 AC 443 ms
57,132 KB
testcase_19 AC 429 ms
57,332 KB
testcase_20 AC 437 ms
57,208 KB
testcase_21 AC 459 ms
59,420 KB
testcase_22 AC 339 ms
56,028 KB
testcase_23 AC 433 ms
57,540 KB
testcase_24 AC 293 ms
53,688 KB
testcase_25 AC 427 ms
57,480 KB
testcase_26 AC 467 ms
57,424 KB
testcase_27 AC 457 ms
57,364 KB
testcase_28 AC 329 ms
56,028 KB
testcase_29 AC 444 ms
57,224 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:193:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debug(a: LongArray) {
          ^
Main.kt:197:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debug(a: IntArray) {
          ^
Main.kt:201:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debug(a: BooleanArray) {
          ^
Main.kt:205:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun toString(a: BooleanArray) = run{a.map { if (it) 1 else 0 }.joinToString("")}
          ^
Main.kt:207:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debugDim(A: Array<LongArray>) {
          ^
Main.kt:214:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debugDim(A: Array<IntArray>) {
          ^
Main.kt:221:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debugDim(A: Array<BooleanArray>) {
          ^
Main.kt:238:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun assert(b: Boolean) = run{if (!b) throw AssertionError()}
          ^

ソースコード

diff #

import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader
import java.lang.AssertionError
import java.util.*
import kotlin.math.*

val MOD = 1_000_000_007


/**
 * @param mod 逆数計算するため素数でないといけない
 */
class Comb(n: Int, val mod: Int) {

  val F = LongArray(n + 1)
  val I = LongArray(n + 1)

  // n>=modでも処理できるように、*modを別扱いにする
  val cntF = IntArray(n + 1) // Fの*modの数
  val x = IntArray(n + 1) // 数値から*modを取り除いた値
  val cntX = IntArray(n + 1) // xの*modの数

  init {
    F[0] = 1
    I[0] = 1
    for (i in 1..n) {
      x[i] = i
      if(x[i] % mod == 0) {
        val j = i/mod
        cntX[i] = cntX[j] + 1
        x[i] = x[j]
      }
      F[i] = F[i - 1] * x[i] % mod
      cntF[i] += cntF[i - 1] + cntX[i]
    }

    I[n] = powMod(F[n], (mod - 2).toLong(), mod)
    for (i in n - 1 downTo 0) {
      I[i] = I[i + 1] * x[i + 1] % mod
    }
  }

  private fun powMod(a: Long, n: Long, mod: Int): Long {
    if (n == 0L) return 1
    val res = powMod(a * a % mod, n / 2, mod)
    return if (n % 2 == 1L) res * a % mod else res
  }

  fun comb(n: Int, k: Int): Long {
    if (n < k) return 0L
    val c = cntF[n] - cntF[k] - cntF[n - k]
    return if (c > 0) 0 else F[n] * I[k] % mod * I[n - k] % mod
  }

  fun perm(n: Int, k: Int): Long {
    val c = cntF[n] - cntF[n - k]
    return if (c > 0) 0 else F[n] * I[n - k] % mod
  }

  fun inv(x: Int): Long {
    val c = cntF[x] - cntF[x - 1]
    return if (c > 0) 0 else I[x] * F[x - 1] % mod
  }

  /**
   * nのグループからk回重複ありで選ぶ組み合わせ数
   * n - 1のしきりとkの○で考える
   */
  fun H(n: Int, k: Int) = comb(n + k - 1, k)
}
class Solver(stream: InputStream, private val out: java.io.PrintWriter) {
  private val reader = BufferedReader(InputStreamReader(stream), 32768)

  fun solve() {
    val N = ni()
    val M = nl()
    val A = intArrayOf(0) + na(N - 1)
    debug(A)

    val C = LongArray(N - 1)
    for (i in 0 until N - 1) {
      C[i] = (MOD + A[i + 1] - A[i]).toLong() % MOD
    }
    debug(C)

    val comb = Comb(N, MOD)

    var ans = 0L
    var cur = 1L
    for (i in 0 until N - 1) {
      val n = i + 1
      val mul = (M+n) %MOD
      val div = comb.inv(n)
      cur = cur *mul%MOD *div%MOD
      val cont = (MOD + cur - 1) % MOD
      ans += C[N - 2 - i] *cont%MOD
    }

    out.println(ans % MOD)
  }













































  private val isDebug = try {
    // なんか本番でエラーでる
    System.getenv("MY_DEBUG") != null
  } catch (t: Throwable) {
    false
  }

  private var tokenizer: StringTokenizer? = null
  private fun next(): String {
    while (tokenizer == null || !tokenizer!!.hasMoreTokens()) {
      tokenizer = StringTokenizer(reader.readLine())
    }
    return tokenizer!!.nextToken()
  }

  private fun ni() = next().toInt()
  private fun nl() = next().toLong()
  private fun ns() = next()
  private fun na(n: Int, offset: Int = 0): IntArray {
    return IntArray(n) { ni() + offset }
  }
  private fun nal(n: Int, offset: Int = 0): LongArray {
    val res = LongArray(n)
    for (i in 0 until n) {
      res[i] = nl() + offset
    }
    return res
  }

  private fun na2(n: Int, offset: Int = 0): Array<IntArray> {
    val a  = Array(2){IntArray(n)}
    for (i in 0 until n) {
      for (e in a) {
        e[i] = ni() + offset
      }
    }
    return a
  }

  private inline fun debug(msg: () -> String) {
    if (isDebug) System.err.println(msg())
  }

  /**
   * コーナーケースでエラー出たりするので、debug(dp[1])のように添え字付きの場合はdebug{}をつかうこと
   */
  private inline fun debug(a: LongArray) {
    debug { a.joinToString(" ") }
  }

  private inline fun debug(a: IntArray) {
    debug { a.joinToString(" ") }
  }

  private inline fun debug(a: BooleanArray) {
    debug { toString(a) }
  }

  private inline fun toString(a: BooleanArray) = run{a.map { if (it) 1 else 0 }.joinToString("")}

  private inline fun debugDim(A: Array<LongArray>) {
    if (isDebug) {
      for (a in A) {
        debug(a)
      }
    }
  }
  private inline fun debugDim(A: Array<IntArray>) {
    if (isDebug) {
      for (a in A) {
        debug(a)
      }
    }
  }
  private inline fun debugDim(A: Array<BooleanArray>) {
    if (isDebug) {
      for (a in A) {
        debug(a)
      }
    }
  }

  /**
   * 勝手にimport消されるのを防ぎたい
   */
  private fun hoge() {
    min(1, 2)
    max(1, 2)
    abs(-10)
  }

  private inline fun assert(b: Boolean) = run{if (!b) throw AssertionError()}
  private inline fun assert(b: Boolean, f: () -> String) = run{if (!b) throw AssertionError(f())}
}

fun main() {
  val out = java.io.PrintWriter(System.out)
  Solver(System.`in`, out).solve()
  out.flush()
}
0