結果

問題 No.1036 Make One With GCD 2
ユーザー yudedakoyudedako
提出日時 2022-01-11 11:53:25
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 1,801 ms / 2,000 ms
コード長 1,304 bytes
コンパイル時間 12,607 ms
コンパイル使用メモリ 439,540 KB
実行使用メモリ 195,384 KB
最終ジャッジ日時 2024-04-26 20:38:13
合計ジャッジ時間 57,806 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,057 ms
163,892 KB
testcase_01 AC 1,016 ms
181,216 KB
testcase_02 AC 1,142 ms
195,384 KB
testcase_03 AC 611 ms
94,732 KB
testcase_04 AC 710 ms
133,968 KB
testcase_05 AC 301 ms
57,020 KB
testcase_06 AC 296 ms
57,112 KB
testcase_07 AC 709 ms
98,180 KB
testcase_08 AC 665 ms
86,548 KB
testcase_09 AC 1,208 ms
178,740 KB
testcase_10 AC 1,135 ms
155,496 KB
testcase_11 AC 1,208 ms
176,280 KB
testcase_12 AC 1,149 ms
160,756 KB
testcase_13 AC 1,486 ms
166,152 KB
testcase_14 AC 1,501 ms
166,504 KB
testcase_15 AC 1,445 ms
167,064 KB
testcase_16 AC 1,456 ms
167,536 KB
testcase_17 AC 1,477 ms
168,988 KB
testcase_18 AC 340 ms
57,328 KB
testcase_19 AC 349 ms
57,432 KB
testcase_20 AC 376 ms
57,336 KB
testcase_21 AC 376 ms
57,212 KB
testcase_22 AC 1,424 ms
166,616 KB
testcase_23 AC 1,220 ms
154,124 KB
testcase_24 AC 1,474 ms
168,280 KB
testcase_25 AC 1,384 ms
172,848 KB
testcase_26 AC 1,435 ms
176,060 KB
testcase_27 AC 296 ms
56,944 KB
testcase_28 AC 299 ms
56,988 KB
testcase_29 AC 306 ms
57,096 KB
testcase_30 AC 299 ms
57,208 KB
testcase_31 AC 308 ms
57,140 KB
testcase_32 AC 318 ms
57,196 KB
testcase_33 AC 299 ms
57,096 KB
testcase_34 AC 316 ms
57,148 KB
testcase_35 AC 301 ms
57,064 KB
testcase_36 AC 299 ms
57,192 KB
testcase_37 AC 299 ms
57,100 KB
testcase_38 AC 1,141 ms
194,928 KB
testcase_39 AC 1,146 ms
194,860 KB
testcase_40 AC 1,217 ms
154,108 KB
testcase_41 AC 1,801 ms
194,968 KB
testcase_42 AC 1,798 ms
194,952 KB
testcase_43 AC 1,645 ms
194,840 KB
testcase_44 AC 1,738 ms
195,056 KB
権限があれば一括ダウンロードができます

ソースコード

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).toLongArray()
    val table = mutableListOf(num)
    while ((1 shl table.size) <= n) {
        val len = 1 shl table.size
        val dest = LongArray(n - len + 1)
        for (i in 0 .. n - len) {
            dest[i] = 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