結果

問題 No.1036 Make One With GCD 2
ユーザー yudedakoyudedako
提出日時 2022-01-11 11:51:08
言語 Kotlin
(1.9.23)
結果
TLE  
実行時間 -
コード長 1,289 bytes
コンパイル時間 12,818 ms
コンパイル使用メモリ 445,880 KB
実行使用メモリ 303,760 KB
最終ジャッジ日時 2024-04-26 20:37:07
合計ジャッジ時間 26,355 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,985 ms
296,360 KB
testcase_01 AC 1,104 ms
164,388 KB
testcase_02 TLE -
testcase_03 AC 646 ms
89,400 KB
testcase_04 AC 749 ms
113,828 KB
testcase_05 AC 308 ms
57,004 KB
testcase_06 AC 301 ms
57,204 KB
testcase_07 AC 747 ms
103,524 KB
testcase_08 AC 712 ms
105,524 KB
testcase_09 AC 1,289 ms
167,008 KB
testcase_10 AC 1,217 ms
148,580 KB
testcase_11 AC 1,326 ms
169,124 KB
testcase_12 AC 1,225 ms
148,608 KB
testcase_13 AC 1,590 ms
166,896 KB
testcase_14 AC 1,593 ms
167,176 KB
testcase_15 AC 1,548 ms
165,088 KB
testcase_16 AC 1,533 ms
165,352 KB
testcase_17 AC 1,562 ms
167,084 KB
testcase_18 AC 345 ms
57,204 KB
testcase_19 AC 358 ms
57,484 KB
testcase_20 AC 390 ms
59,456 KB
testcase_21 AC 379 ms
59,356 KB
testcase_22 AC 1,526 ms
165,076 KB
testcase_23 AC 1,239 ms
117,392 KB
testcase_24 AC 1,567 ms
167,000 KB
testcase_25 AC 1,484 ms
165,120 KB
testcase_26 AC 1,522 ms
167,092 KB
testcase_27 AC 302 ms
57,124 KB
testcase_28 AC 298 ms
57,052 KB
testcase_29 AC 302 ms
57,200 KB
testcase_30 AC 303 ms
56,988 KB
testcase_31 AC 323 ms
57,464 KB
testcase_32 AC 324 ms
57,164 KB
testcase_33 AC 304 ms
56,860 KB
testcase_34 AC 318 ms
57,204 KB
testcase_35 AC 305 ms
57,200 KB
testcase_36 AC 305 ms
57,292 KB
testcase_37 AC 303 ms
56,992 KB
testcase_38 TLE -
testcase_39 AC 1,614 ms
222,400 KB
testcase_40 AC 1,255 ms
117,404 KB
testcase_41 TLE -
testcase_42 TLE -
testcase_43 TLE -
testcase_44 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

tailrec fun gcd(a: Long, b: Long): Long {
    return when(b){
        0L -> a
        else -> gcd(b, a % b)
    }
}
fun Int.floorLog2(): Int {
    var rest = this
    var result = 0
    for (d in 4 downTo 0) {
        if (rest >= (1 shl (1 shl d))) {
            rest = rest shr (1 shl d)
            result += 1 shl d
        }
    }
    return result
}
fun main() {
    val n = readLine()!!.trim().toInt()
    val num = readLine()!!.trim().split(' ').map(String::toLong)
    val table = mutableListOf(num)
    while ((1 shl table.size) <= n) {
        val len = 1 shl table.size
        val dest = mutableListOf<Long>()
        for (i in 0 .. n - len) {
            dest.add(gcd(table.last()[i], table.last()[i + len / 2]))
        }
        table.add(dest)
    }
    fun readTable(from: Int, until: Int): Long {
        val len = until - from
        if (len == 0) return 0L
        val log2 = len.floorLog2()
        return gcd(table[log2][from], table[log2][from + len - (1 shl log2)])
    }
    var j = 0
    var result = 0L
    for (i in 0 until n) {
        var d = readTable(i, j)
        while (j in num.indices && d != 1L) {
            d = gcd(d, num[j])
            j += 1
        }
        if (d == 1L) {
            result += n - j + 1
        }
    }
    println(result)
}
0