Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fun main() {
- val s1 = readLine()!!.split(' ').map { it.toInt() }
- val n: Int = s1[0]
- val k: Int = s1[1]
- val permutationS = (readLine() ?: "").split(' ')
- val permutation = permutationS.map { it.toInt() }
- var listOfLessValues = mutableListOf<Int>()
- var valueToLessCount: SortedMap<Int, Int> = TreeMap(
- compareByDescending { it }
- )
- var index = -1
- for (i in permutation.size - 1 downTo 0) {
- val lastValue = getAddingValue(permutation[i], valueToLessCount)
- if (haveIsLess(permutation[i], valueToLessCount)) {
- listOfLessValues.add(1 + lastValue)
- valueToLessCount[permutation[i]] = 1 + lastValue
- } else {
- listOfLessValues.add(0)
- valueToLessCount[permutation[i]] = 0
- }
- ++index
- }
- println(listOfLessValues)
- var sum = 0
- for(i in listOfLessValues.indices) {
- if(listOfLessValues[i] >= k-1) {
- sum += countCombinations(listOfLessValues[i], k-1)
- }
- }
- println(sum)
- }
- fun getAddingValue(value: Int, map: Map<Int, Int>): Int {
- return map.entries.firstOrNull { it.key < value }?.value ?: 0
- }
- fun haveIsLess(value: Int, map: Map<Int, Int>): Boolean {
- return map.entries.firstOrNull { value > it.key } != null
- }
- fun countCombinations(n: Int, m: Int): Int {
- var nMinusMToN = BigInteger.valueOf(1)
- for (i in (n - m + 1)..n) {
- nMinusMToN = nMinusMToN.multiply(i.toBigInteger())
- }
- for (i in 1..m) {
- nMinusMToN = nMinusMToN.divide(i.toBigInteger())
- }
- val module = BigInteger.valueOf(10e8.toLong() + 7L)
- return nMinusMToN.mod(module).toInt()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement