結果

問題 No.1243 約数加算
ユーザー rutilicusrutilicus
提出日時 2020-10-16 10:16:04
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 346 ms / 2,000 ms
コード長 950 bytes
コンパイル時間 11,367 ms
コンパイル使用メモリ 418,604 KB
実行使用メモリ 55,864 KB
最終ジャッジ日時 2023-09-28 01:46:29
合計ジャッジ時間 15,428 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 261 ms
53,116 KB
testcase_01 AC 261 ms
52,948 KB
testcase_02 AC 267 ms
52,920 KB
testcase_03 AC 263 ms
52,788 KB
testcase_04 AC 282 ms
52,968 KB
testcase_05 AC 321 ms
55,008 KB
testcase_06 AC 341 ms
55,864 KB
testcase_07 AC 294 ms
53,288 KB
testcase_08 AC 346 ms
55,644 KB
testcase_09 AC 268 ms
52,968 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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(" "))
                ^

ソースコード

diff #

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)
    }
0