Skip to content

Instantly share code, notes, and snippets.

@omo
Created August 16, 2022 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omo/060530ae0f1aafe95b3a63387de33ab7 to your computer and use it in GitHub Desktop.
Save omo/060530ae0f1aafe95b3a63387de33ab7 to your computer and use it in GitHub Desktop.
import kotlin.math.*
fun debugln(str: String) = println(str)
//fun debugln(@Suppress("UNUSED_PARAMETER") unused: String) = {}
fun search(n: Int) : List<Int> {
val ans = (0 until n).toMutableList()
fun fill(r: Int) {
if (r < 0) {
return
}
// The sqrt that should exist.
val s = sqrt(2.0 * r).toInt().let { it * it }
assert(r < s && s < 2 * r)
// ??? Why is this range?
val l = s - r
(l .. r).forEach { ans[it] = s - it }
// Recurse down to the remaining range.
fill(l - 1)
debugln("$l to $r is filled with $s")
}
fill(n - 1)
return ans
}
fun main() {
repeat(readLine()!!.toInt()) {
val len = readLine()!!.toInt()
println(search(len).joinToString(separator=" "))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment