Skip to content

Instantly share code, notes, and snippets.

View crakaC's full-sized avatar

K.SHIRAKASHI crakaC

View GitHub Profile
@crakaC
crakaC / mat.kt
Created January 25, 2021 08:22
競プロでアフィン変換が出てきたときに使う行列
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{
@crakaC
crakaC / extGCD.kt
Last active December 21, 2020 06:40
拡張ユークリッドの互除法
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()}
@crakaC
crakaC / ceil.kt
Last active February 7, 2021 05:25
ceil
//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
@crakaC
crakaC / UnionFindTree.kt
Last active November 16, 2020 09:06
KotlinでUnion-Find木
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)
@crakaC
crakaC / multi_set.kt
Created November 11, 2020 08:17
KotlinでMultiSetを雑に実装
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
@crakaC
crakaC / various_map_test.kt
Last active November 8, 2020 10:55
各種Mapへの要素追加、ランダムアクセス、イテレーションの実行速度(Kotlin)
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(){
@crakaC
crakaC / permutation_and_combination.kt
Last active November 1, 2020 07:57
順列、組み合わせの計算するやつ
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
@crakaC
crakaC / mpow.kt
Created October 31, 2020 17:23
高速に累乗計算するやつ
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)
@crakaC
crakaC / makefile
Last active October 30, 2020 12:46
Kotlinのコードをさっと実行するためのmakefile
.PRECIOUS: %.jar
%.jar: %.kt
kotlinc $< -include-runtime -d $@
%: %.jar
kotlin $<
.PHONY: clean
clean:
@crakaC
crakaC / pair_array_sort.kt
Created October 22, 2020 11:44
Array<Pair<Int, Int>をソートするやつ
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"))
}