Advertisement
kish-dev

Untitled

Mar 15th, 2022
1,216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.68 KB | None | 0 0
  1. fun main() {
  2.     val s1 = readLine()!!.split(' ').map { it.toInt() }
  3.     val n: Int = s1[0]
  4.     val k: Int = s1[1]
  5.  
  6.     val permutationS = (readLine() ?: "").split(' ')
  7.         val permutation = permutationS.map { it.toInt() }
  8.  
  9.     var listOfLessValues = mutableListOf<Int>()
  10.     var valueToLessCount: SortedMap<Int, Int> = TreeMap(
  11.         compareByDescending { it }
  12.     )
  13.     var index = -1
  14.     for (i in permutation.size - 1 downTo 0) {
  15.         val lastValue = getAddingValue(permutation[i], valueToLessCount)
  16.         if (haveIsLess(permutation[i], valueToLessCount)) {
  17.             listOfLessValues.add(1 + lastValue)
  18.             valueToLessCount[permutation[i]] = 1 + lastValue
  19.         } else {
  20.             listOfLessValues.add(0)
  21.             valueToLessCount[permutation[i]] = 0
  22.         }
  23.         ++index
  24.     }
  25.     println(listOfLessValues)
  26.     var sum = 0
  27.     for(i in listOfLessValues.indices) {
  28.         if(listOfLessValues[i] >= k-1) {
  29.             sum += countCombinations(listOfLessValues[i], k-1)
  30.         }
  31.     }
  32.     println(sum)
  33. }
  34.  
  35. fun getAddingValue(value: Int, map: Map<Int, Int>): Int {
  36.     return map.entries.firstOrNull { it.key < value }?.value ?: 0
  37. }
  38.  
  39. fun haveIsLess(value: Int, map: Map<Int, Int>): Boolean {
  40.     return map.entries.firstOrNull { value > it.key } != null
  41. }
  42.  
  43. fun countCombinations(n: Int, m: Int): Int {
  44.     var nMinusMToN = BigInteger.valueOf(1)
  45.  
  46.     for (i in (n - m + 1)..n) {
  47.         nMinusMToN = nMinusMToN.multiply(i.toBigInteger())
  48.     }
  49.  
  50.     for (i in 1..m) {
  51.         nMinusMToN = nMinusMToN.divide(i.toBigInteger())
  52.     }
  53.     val module = BigInteger.valueOf(10e8.toLong() + 7L)
  54.     return nMinusMToN.mod(module).toInt()
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement