結果
| 問題 |
No.1243 約数加算
|
| コンテスト | |
| ユーザー |
rutilicus
|
| 提出日時 | 2020-10-16 10:16:04 |
| 言語 | Kotlin (2.1.0) |
| 結果 |
AC
|
| 実行時間 | 371 ms / 2,000 ms |
| コード長 | 950 bytes |
| コンパイル時間 | 11,099 ms |
| コンパイル使用メモリ | 444,412 KB |
| 実行使用メモリ | 54,064 KB |
| 最終ジャッジ日時 | 2024-07-20 20:23:13 |
| 合計ジャッジ時間 | 15,629 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 9 |
コンパイルメッセージ
Main.kt:22:17: warning: 'appendln(Int): kotlin.text.StringBuilder /* = java.lang.StringBuilder */' is deprecated. Use appendLine instead. Note that the new method always appends the line feed character '\n' regardless of the system line separator.
builder.appendln(ans.size + ansRev.size)
^
Main.kt:26:17: warning: 'appendln(String?): kotlin.text.StringBuilder /* = java.lang.StringBuilder */' is deprecated. Use appendLine instead. Note that the new method always appends the line feed character '\n' regardless of the system line separator.
builder.appendln(ansRev.reversed().joinToString(" "))
^
ソースコード
fun main() {
val builder = StringBuilder()
val n = readInputLine().toInt()
repeat(n) {
var (a, b) = readInputLine().split(" ").map { it.toLong() }
val ans = mutableListOf<Long>()
val ansRev = mutableListOf<Long>()
while (a != b) {
val gcd = gcd(a, b)
if ((a / gcd) % 2L == 1L) {
ans.add(gcd)
a += gcd
} else {
ansRev.add(gcd)
b -= gcd
}
}
builder.appendln(ans.size + ansRev.size)
if (ans.isNotEmpty()) {
builder.append(ans.joinToString(" ") + " ")
}
builder.appendln(ansRev.reversed().joinToString(" "))
}
print(builder.toString())
}
fun readInputLine(): String {
return readLine()!!
}
tailrec fun gcd(a: Long, b: Long): Long =
when {
b > a -> gcd(b, a)
b == 0L -> a
else -> gcd(b, a % b)
}
rutilicus