This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class Mat( | |
val r1: LongArray, | |
val r2: LongArray, | |
val r3: LongArray = longArrayOf(0L, 0L, 1L) | |
){ | |
val rows = listOf(r1, r2, r3) | |
val cols = listOf(c(0), c(1), c(2)) | |
fun c(i: Int): LongArray = rows.map{it[i]}.toLongArray() | |
operator fun times(other: Mat): Mat{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun extGCD(a: Long, b: Long): List<Long> { | |
if(b == 0L){ | |
return listOf(a, 1, 0) | |
} | |
val(d, y, x) = extGCD(b, a % b) | |
return listOf(d, x, y - a / b * x) | |
} | |
fun main(){ | |
val(a, b) = readLine()!!.split(" ").map{it.toLong()} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//ceil(n / k) | |
(n + k - 1) / k | |
fun floor(x: Long) = if(x > 0L) x / K * K else (x - K + 1) / K * K | |
fun ceil(x: Long) = if(x > 0L) (x + K - 1) / K * K else x / K * K |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UF(n: Int){ | |
val r = IntArray(n){-1} | |
fun root(x: Int): Int { | |
if(r[x] < 0) return x | |
r[x] = root(r[x]) | |
return r[x] | |
} | |
fun unite(x: Int, y: Int){ | |
val a = root(x) | |
val b = root(y) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.TreeMap | |
import java.util.TreeSet | |
class MultiSet<T>( | |
comparator: Comparator<T>? = null, | |
val map: TreeMap<T, Int> = TreeMap<T, Int>(comparator) | |
) :MutableSet<T> by map.keys{ | |
override fun add(element: T): Boolean { | |
map[element] = (map[element]?: 0) + 1 | |
return true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kotlin.system.* | |
import kotlin.random.Random | |
import java.lang.Math.* | |
val N = 100_000 | |
val M = 100 | |
var randomArray = Array(M){IntArray(N){Random.nextInt(N)}} | |
fun main(){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const val MOD = 1_000_000_007L | |
fun mul(a: Long, b: Long) = a * b % MOD | |
fun mulAll(vararg n: Long) = n.reduce(::mul) | |
fun mpow(a: Int, x: Int) = mpow(a.toLong(), x.toLong()) | |
fun mpow(a: Long, x: Long): Long{ | |
var n = a | |
var k = x | |
var ret = 1L |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const val MOD = 1_000_000_007L | |
fun mul(a: Long, b: Long) = a * b % MOD | |
fun mpow(n: Int, x: Int) = mpow(n.toLong(), x.toLong()) | |
fun mpow(n: Long, x: Long): Long{ | |
var a = n | |
var k = x | |
var ret = 1L | |
while(k > 0L){ | |
if(k % 2 == 1L){ | |
ret = mul(ret, a) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.PRECIOUS: %.jar | |
%.jar: %.kt | |
kotlinc $< -include-runtime -d $@ | |
%: %.jar | |
kotlin $< | |
.PHONY: clean | |
clean: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kotlin.system.* | |
import kotlin.random.Random | |
fun main(){ | |
val pairs = Array(10){ | |
Random.nextInt(10) to Random.nextInt(10) | |
}.sortedWith(compareBy({it.first}, {it.second})) | |
println(pairs.joinToString("\n")) | |
} |