結果

問題 No.1006 Share an Integer
ユーザー 箱星
提出日時 2020-03-06 21:57:21
言語 Kotlin
(2.1.0)
結果
WA  
実行時間 -
コード長 1,542 bytes
コンパイル時間 14,791 ms
コンパイル使用メモリ 444,340 KB
実行使用メモリ 96,276 KB
最終ジャッジ日時 2024-10-14 06:44:16
合計ジャッジ時間 22,883 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 3
other WA * 8 TLE * 1 -- * 10
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:7:10: warning: parameter 'args' is never used
fun main(args: Array<String>) {
         ^
Main.kt:41:12: 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.
        sb.appendln("%d %d".format(item.first, item.second))
           ^

ソースコード

diff #

import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader
import java.lang.StringBuilder
import java.util.*

fun main(args: Array<String>) {
    val sc = FastScanner(System.`in`)
    val x = sc.nextInt()
    val count = Array(x) { 0 }
    for (n in 1 until x) {
        var d = 1
        while (d * d <= n) {
            if (n % d == 0) {
                if (d * d != n) {
                    count[n] += 2
                } else {
                    count[n] += 1
                }
            }
            d++
        }
    }
    var min = Int.MAX_VALUE
    val ans = mutableListOf<Pair<Int, Int>>()
    for (a in 1..x / 2) {
        val b = x - a
        val v = Math.abs((a - count[a]) - (b - count[b]))
        if (min > v) {
            min = v
            ans.clear()
        }
        if (min == v) {
            ans.add(a to b)
            ans.add(b to a)
        }
    }
    ans.sortBy { it.first }
    val sb = StringBuilder()
    for (item in ans) {
        sb.appendln("%d %d".format(item.first, item.second))
    }
    println(sb.toString())
}

class FastScanner(s: InputStream) {
    private var st = StringTokenizer("")
    private val br = BufferedReader(InputStreamReader(s))

    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()
    fun ready() = br.ready()
}
0