結果

問題 No.1339 循環小数
ユーザー 👑 箱
提出日時 2021-01-15 22:07:10
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 459 ms / 2,000 ms
コード長 2,012 bytes
コンパイル時間 14,590 ms
コンパイル使用メモリ 438,776 KB
実行使用メモリ 53,484 KB
最終ジャッジ日時 2024-05-04 23:57:52
合計ジャッジ時間 29,185 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 298 ms
50,584 KB
testcase_01 AC 307 ms
50,672 KB
testcase_02 AC 306 ms
50,716 KB
testcase_03 AC 308 ms
50,864 KB
testcase_04 AC 306 ms
51,116 KB
testcase_05 AC 305 ms
50,864 KB
testcase_06 AC 306 ms
50,652 KB
testcase_07 AC 304 ms
50,948 KB
testcase_08 AC 305 ms
50,792 KB
testcase_09 AC 306 ms
50,824 KB
testcase_10 AC 308 ms
51,080 KB
testcase_11 AC 311 ms
50,996 KB
testcase_12 AC 312 ms
50,972 KB
testcase_13 AC 314 ms
51,092 KB
testcase_14 AC 315 ms
50,976 KB
testcase_15 AC 312 ms
50,956 KB
testcase_16 AC 314 ms
51,040 KB
testcase_17 AC 314 ms
50,988 KB
testcase_18 AC 317 ms
50,724 KB
testcase_19 AC 314 ms
50,964 KB
testcase_20 AC 315 ms
50,828 KB
testcase_21 AC 403 ms
52,504 KB
testcase_22 AC 401 ms
52,612 KB
testcase_23 AC 406 ms
52,676 KB
testcase_24 AC 392 ms
53,096 KB
testcase_25 AC 406 ms
52,492 KB
testcase_26 AC 409 ms
52,928 KB
testcase_27 AC 432 ms
53,484 KB
testcase_28 AC 405 ms
52,668 KB
testcase_29 AC 400 ms
53,040 KB
testcase_30 AC 404 ms
53,088 KB
testcase_31 AC 459 ms
52,636 KB
testcase_32 AC 454 ms
52,632 KB
testcase_33 AC 398 ms
53,016 KB
testcase_34 AC 352 ms
51,760 KB
testcase_35 AC 411 ms
51,868 KB
testcase_36 AC 406 ms
53,060 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

fun phi(n: Long): Long {
    var res = n
    var i = 2L
    var m = n
    while (i * i <= m) {
        if (m % i == 0L) {
            res = res / i * (i - 1)
            while (m % i == 0L) {
                m /= i
            }
        }
        i++
    }
    if (m > 1) {
        res = res / m * (m - 1)
    }
    return res
}

fun modpow(a: Long, n: Long, m: Long): Long {
    if (n == 0L) return 1
    if (n == 1L) return a % m
    val pow = modpow(a, n / 2, m)
    var res = pow * pow % m
    if (n % 2 == 1L) {
        res *= a
        res %= m
    }
    return res
}

fun PrintWriter.solve() {
    val numCases = nextInt()
    case@ for (_i in 0 until numCases) {
        var n = nextLong()
        while (n % 2 == 0L) n /= 2
        while (n % 5 == 0L) n /= 5
        if (n == 1L) {
            println(1)
            continue@case
        }
        val phi = phi(n)
        // region divisors
        val number = phi
        val divs = mutableListOf<Long>()
        var divisor = 1L
        while (divisor * divisor <= number) {
            if (number % divisor == 0L) {
                divs.add(divisor)
                if (divisor * divisor != number) {
                    divs.add(number / divisor)
                }
            }
            divisor++
        }
        divs.sort()
        // endregion
        for (m in divs) {
            if (modpow(10, m, n) == 1L) {
                println(m)
                continue@case
            }
        }
    }
}

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