結果

問題 No.1200 お菓子配り-3
ユーザー 箱星
提出日時 2020-08-28 22:06:07
言語 Kotlin
(2.1.0)
結果
AC  
実行時間 2,518 ms / 4,000 ms
コード長 1,706 bytes
コンパイル時間 13,179 ms
コンパイル使用メモリ 436,800 KB
実行使用メモリ 83,848 KB
最終ジャッジ日時 2024-11-20 22:30:27
合計ジャッジ時間 45,100 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader
import java.io.PrintWriter
import java.util.*

fun PrintWriter.solve(sc: FastScanner) {
    val s = sc.nextInt()
    for (_i in 0 until s) {
        val x = sc.nextInt()
        val y = sc.nextInt()
        val cand_a = mutableSetOf<Long>()
        val n1 = x + y
        var d = 1
        while (d * d <= n1) {
            if (n1 % d == 0) {
                cand_a.add(d - 1L)
                cand_a.add(n1 / d - 1L)
            }
            d++
        }
        val n2 = Math.abs(x - y)
        d = 1
        while (d * d <= n2) {
            if (n2 % d == 0) {
                cand_a.add(d + 1L)
                cand_a.add(n2 / d + 1L)
            }
            d++
        }
        cand_a.remove(0)
        cand_a.remove(1)
        var ans = 0L
        for (a in cand_a) {
            val num = a * a - 1
            if (a * x > y && a * y > x && (a * x - y) % num == 0L && (a * y - x) % num == 0L) {
                ans++
            }
        }
        if (x == y) {
            ans += x - 1
        }
        println(ans)
    }
}

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

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