結果

問題 No.1596 Distance Sum in 2D Plane
ユーザー yakamotoyakamoto
提出日時 2021-07-10 17:58:36
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 787 ms / 2,000 ms
コード長 4,883 bytes
コンパイル時間 17,731 ms
コンパイル使用メモリ 472,056 KB
実行使用メモリ 98,908 KB
最終ジャッジ日時 2024-07-02 02:42:22
合計ジャッジ時間 31,025 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 324 ms
60,924 KB
testcase_01 AC 324 ms
60,852 KB
testcase_02 AC 786 ms
98,616 KB
testcase_03 AC 777 ms
98,908 KB
testcase_04 AC 787 ms
98,704 KB
testcase_05 AC 784 ms
98,556 KB
testcase_06 AC 775 ms
98,592 KB
testcase_07 AC 774 ms
98,540 KB
testcase_08 AC 771 ms
98,684 KB
testcase_09 AC 778 ms
98,616 KB
testcase_10 AC 776 ms
98,480 KB
testcase_11 AC 727 ms
98,696 KB
testcase_12 AC 725 ms
98,764 KB
testcase_13 AC 764 ms
98,356 KB
testcase_14 AC 292 ms
49,744 KB
testcase_15 AC 293 ms
49,804 KB
testcase_16 AC 290 ms
49,892 KB
testcase_17 AC 291 ms
49,732 KB
testcase_18 AC 294 ms
49,824 KB
testcase_19 AC 300 ms
49,772 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:191: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:195: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:199: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:203: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:205: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:212: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:219: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:236: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.abs
import kotlin.math.max
import kotlin.math.min
import kotlin.math.sqrt

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, M) = na(2)
    val comb = Comb(2*N, MOD)
    var ans = 2*N * comb.comb(2*N, N) %MOD

    for (m in 0 until M) {
      val t = ni()
      val (x, y) = na(2)
      val remain = 2*N - (x + y + 1)
      val minus = when(t) {
        1 -> {
          comb.comb(x + y, x) * comb.comb(remain, N - y) %MOD
        }
        else -> {
          comb.comb(x + y, x) * comb.comb(remain,N - (y + 1)) %MOD
        }
      }
      ans = (ans + MOD - minus) %MOD
    }

    out.println(ans)
  }













































  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