結果

問題 No.1661 Sum is Prime (Hard Version)
ユーザー 👑 箱星箱星
提出日時 2021-08-27 04:39:20
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 1,660 ms / 3,000 ms
コード長 2,326 bytes
コンパイル時間 15,539 ms
コンパイル使用メモリ 463,264 KB
実行使用メモリ 130,300 KB
最終ジャッジ日時 2024-05-01 01:11:56
合計ジャッジ時間 36,312 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 322 ms
60,396 KB
testcase_01 AC 325 ms
60,236 KB
testcase_02 AC 1,270 ms
115,452 KB
testcase_03 AC 285 ms
54,288 KB
testcase_04 AC 327 ms
60,408 KB
testcase_05 AC 326 ms
60,552 KB
testcase_06 AC 326 ms
60,400 KB
testcase_07 AC 324 ms
60,556 KB
testcase_08 AC 325 ms
60,368 KB
testcase_09 AC 328 ms
60,420 KB
testcase_10 AC 335 ms
60,468 KB
testcase_11 AC 323 ms
60,392 KB
testcase_12 AC 1,195 ms
111,320 KB
testcase_13 AC 1,169 ms
109,420 KB
testcase_14 AC 1,243 ms
115,356 KB
testcase_15 AC 1,387 ms
125,180 KB
testcase_16 AC 1,281 ms
115,760 KB
testcase_17 AC 1,262 ms
111,524 KB
testcase_18 AC 732 ms
97,816 KB
testcase_19 AC 1,107 ms
111,464 KB
testcase_20 AC 765 ms
96,360 KB
testcase_21 AC 1,082 ms
115,608 KB
testcase_22 AC 1,660 ms
130,300 KB
testcase_23 AC 1,494 ms
127,480 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter
import java.util.*
import kotlin.math.*

fun primePi(n: Long): Long {
    if (n <= 3) return maxOf(0, n - 1)
    val v = floor(sqrt(n.toDouble())).roundToInt()
    val smalls = (0..v).map { (it + 1) / 2 }.toTypedArray()
    var s = (v + 1) / 2
    val roughs = (0 until s).map { 2 * it + 1 }.toTypedArray()
    val larges = (0 until s).map { (n / (2 * it + 1) + 1) / 2 }.toTypedArray()
    val skip = BooleanArray(v + 1) { false }
    var pc = 0
    for (p in 3..v step 2) {
        if (skip[p]) continue
        val q = p * p.toLong()
        pc++
        if (q * q > n) break
        skip[p] = true
        for (i in q..v step 2L * p) {
            skip[i.toInt()] = true
        }
        var ns = 0
        for (k in 0 until s) {
            val i = roughs[k]
            if (skip[i]) continue
            val d = i * p.toLong()
            val x = if (d <= v) larges[smalls[d.toInt()] - pc] else smalls[(n / d).toInt()].toLong()
            larges[ns] = larges[k] + pc - x
            roughs[ns] = i
            ns++
        }
        s = ns
        var i = v
        for (j in (p..v / p).reversed()) {
            val c = smalls[j] - pc
            val e = j * p.toLong()
            while (i >= e) {
                smalls[i] -= c
                i--
            }
        }
    }
    var res = larges[0] + (s + 2L * (pc - 1)) * (s - 1L) / 2 - larges.slice(1 until s).sum()

    for (l in 1 until s) {
        val q = roughs[l]
        val m = n / q
        val e = smalls[(m / q).toInt()] - pc
        if (e <= l) break
        val t = roughs.slice(l + 1..e).map { smalls[(m / it).toInt()].toLong() }.sum()
        res += t - (e - l) * (pc + l - 1L)
    }
    return res
}

fun PrintWriter.solve() {
    val l = nextLong()
    val r = nextLong()
    println(primePi(r) - primePi(l - 1) + primePi(2 * r) - primePi(2 * l))
}

fun main() {
    val writer = PrintWriter(System.out, false)
    writer.solve()
    writer.flush()
}

// region Scanner
private var st = StringTokenizer("")
private val br = System.`in`.bufferedReader()

fun next(): String {
    while (!st.hasMoreTokens()) st = StringTokenizer(br.readLine())
    return st.nextToken()
}

fun nextInt() = next().toInt()
fun nextLong() = next().toLong()
fun nextLine() = br.readLine()
fun nextDouble() = next().toDouble()
// endregion
0