結果

問題 No.1339 循環小数
ユーザー semisagisemisagi
提出日時 2021-01-15 22:26:01
言語 Swift
(5.10.0)
結果
AC  
実行時間 36 ms / 2,000 ms
コード長 4,317 bytes
コンパイル時間 3,848 ms
コンパイル使用メモリ 183,740 KB
実行使用メモリ 13,472 KB
最終ジャッジ日時 2023-08-17 17:44:27
合計ジャッジ時間 5,930 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
13,148 KB
testcase_01 AC 6 ms
13,288 KB
testcase_02 AC 6 ms
13,200 KB
testcase_03 AC 6 ms
13,076 KB
testcase_04 AC 6 ms
13,064 KB
testcase_05 AC 6 ms
13,008 KB
testcase_06 AC 6 ms
12,972 KB
testcase_07 AC 6 ms
12,900 KB
testcase_08 AC 7 ms
13,064 KB
testcase_09 AC 6 ms
12,956 KB
testcase_10 AC 6 ms
13,008 KB
testcase_11 AC 6 ms
13,016 KB
testcase_12 AC 7 ms
13,212 KB
testcase_13 AC 6 ms
13,016 KB
testcase_14 AC 7 ms
13,312 KB
testcase_15 AC 7 ms
13,124 KB
testcase_16 AC 7 ms
13,180 KB
testcase_17 AC 6 ms
13,016 KB
testcase_18 AC 6 ms
13,124 KB
testcase_19 AC 7 ms
12,908 KB
testcase_20 AC 6 ms
13,472 KB
testcase_21 AC 20 ms
12,928 KB
testcase_22 AC 21 ms
13,100 KB
testcase_23 AC 20 ms
13,160 KB
testcase_24 AC 20 ms
12,996 KB
testcase_25 AC 21 ms
13,176 KB
testcase_26 AC 21 ms
13,000 KB
testcase_27 AC 21 ms
13,084 KB
testcase_28 AC 21 ms
13,468 KB
testcase_29 AC 19 ms
12,992 KB
testcase_30 AC 18 ms
13,096 KB
testcase_31 AC 35 ms
13,176 KB
testcase_32 AC 36 ms
13,084 KB
testcase_33 AC 22 ms
12,952 KB
testcase_34 AC 11 ms
13,464 KB
testcase_35 AC 30 ms
12,892 KB
testcase_36 AC 21 ms
13,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import Foundation

struct Scanner {
    private var elements = [String]()
    private var index = 0

    mutating func peek() -> String {
        while elements.count == index {
            elements = readLine()!.split(separator: " ").map(String.init)
            index = 0
        }
        return elements[index]
    }

    mutating func next() -> String {
        defer { index += 1 }
        return peek()
    }

    mutating func nextInt() -> Int {
        return Int(next())!
    }

    mutating func nextInts(_ n: Int) -> [Int] {
        return (0 ..< n).map { _ in nextInt() }
    }

    mutating func nextDouble() -> Double {
        return Double(next())!
    }
}

let mod = 1000000007

struct Zn: CustomStringConvertible, AdditiveArithmetic {
    let n: Int

    init(_ n: Int) {
        self.n = n
    }

    var description: String {
        String(self.n)
    }

    static var zero = Zn(0)
    static var one = Zn(1)

    static func + (lhs: Self, rhs: Self) -> Self {
        let n = lhs.n + rhs.n
        return Zn(n < mod ? n : n - mod)
    }

    static func + (lhs: Self, rhs: Int) -> Self {
        return lhs + Zn(rhs)
    }

    static func + (lhs: Int, rhs: Self) -> Self {
        return Zn(lhs) + rhs
    }

    static func - (lhs: Self, rhs: Self) -> Self {
        let n = lhs.n - rhs.n
        return Zn(n >= 0 ? n : n + mod)
    }

    static func - (lhs: Self, rhs: Int) -> Self {
        return lhs - Zn(rhs)
    }

    static func - (lhs: Int, rhs: Self) -> Self {
        return Zn(lhs) - rhs
    }

    static func * (lhs: Self, rhs: Self) -> Self {
        Zn((lhs.n * rhs.n) % mod)
    }

    static func * (lhs: Self, rhs: Int) -> Self {
        return lhs * Zn(rhs)
    }

    static func * (lhs: Int, rhs: Self) -> Self {
        return Zn(lhs) * rhs
    }

    static func / (lhs: Zn, rhs: Zn) -> Self {
        return lhs * rhs.inverse
    }

    static func / (lhs: Int, rhs: Zn) -> Self {
        return Zn(lhs) * rhs.inverse
    }

    static func / (lhs: Zn, rhs: Int) -> Self {
        return lhs * Zn(rhs).inverse
    }

    static func += (lhs: inout Self, rhs: Self) {
        lhs = lhs + rhs
    }

    static func += (lhs: inout Self, rhs: Int) {
        lhs = lhs + Zn(rhs)
    }

    static func -= (lhs: inout Self, rhs: Self) {
        lhs = lhs - rhs
    }

    static func -= (lhs: inout Self, rhs: Int) {
        lhs = lhs - Zn(rhs)
    }

    static func *= (lhs: inout Self, rhs: Self) {
        lhs = lhs * rhs
    }

    static func *= (lhs: inout Self, rhs: Int) {
        lhs = lhs * Zn(rhs)
    }

    static func /= (lhs: inout Self, rhs: Self) {
        lhs = lhs / rhs
    }

    static func /= (lhs: inout Self, rhs: Int) {
        lhs = lhs / Zn(rhs)
    }

    func pow(_ n: Int) -> Self {
        if n == 0 {
            return Zn(1)
        } else if n % 2 == 1 {
            return self * self.pow(n - 1)
        } else {
            return (self * self).pow(n / 2)
        }
    }

    var inverse: Self {
        self.pow(mod - 2)
    }
}

extension Int {
    func divisors() -> [Int] {
        var i = 1
        var result = [Int]()
        while i * i <= self {
            if self % i == 0 {
                result.append(i)
                if i * i != self {
                    result.append(self / i)
                }
            }
            i += 1
        }
        return result
    }
}

func phi(_ n: Int) -> Int {
    var n = n
    var result = n
    var i = 2
    while i * i <= n {
        if n % i == 0 {
            result = result / i * (i - 1)
            while n % i == 0 {
                n /= i
            }
        }
        i += 1
    }
    if n != 1 {
        result = result / n * (n - 1)
    }
    return result
}

func power(_ n: Int, _ k: Int, _ m: Int) -> Int {
    if k == 0 {
        return 1
    } else if k % 2 == 1 {
        return n * power(n, k - 1, m) % m
    } else {
        return power(n * n % m, k / 2, m)
    }
}

func order(_ n: Int, _ m: Int) -> Int {
    for k in phi(m).divisors().sorted() {
        if power(n, k, m) == 1 {
            return k
        }
    }
    return 1
}

var scanner = Scanner()
let T = scanner.nextInt()
for _ in 0 ..< T {
    var N = scanner.nextInt()
    while N % 2 == 0 {
        N /= 2
    }
    while N % 5 == 0 {
        N /= 5
    }
    print(order(10, N))
}
0