提出 #20355912


ソースコード 拡げる

func readInt1(line: Int = #line, file: String = #file) -> Int {
    guard let string = readLine() else {
        preconditionFailure("No input (at line \(line) in \(file))")
    }
    guard let value = Int(string) else {
        preconditionFailure("Illegal input value: \(string) (at line \(line) in \(file))")
    }
    return value
}

enum LessThanOrEqualToElement {}
func <=(lhs: LessThanOrEqualToElement, rhs: LessThanOrEqualToElement) {}
extension RandomAccessCollection where Index == Int {
    func values(_: (LessThanOrEqualToElement, LessThanOrEqualToElement) -> (), _ predicate: (Element) throws -> Bool) rethrows -> SubSequence {
        var low = startIndex - 1
        var high = endIndex
        while high - low > 1 {
            let mid = low + (high - low) / 2
            if try predicate(self[mid]) {
                low = mid
            } else {
                high = mid
            }
        }
        return self[..<high]
    }
}

infix operator +?: AdditionPrecedence
extension FixedWidthInteger {
    static func +? (lhs: Self, rhs: Self) -> Self? {
        let (result, overflows) = lhs.addingReportingOverflow(rhs)
        if overflows { return nil }
        return result
    }
}

infix operator *?: MultiplicationPrecedence
extension FixedWidthInteger {
    static func *? (lhs: Self, rhs: Self) -> Self? {
        let (result, overflows) = lhs.multipliedReportingOverflow(by: rhs)
        if overflows { return nil }
        return result
    }
}

infix operator ..<?: RangeFormationPrecedence
func ..<?<T>(lhs: T, rhs: T) -> Range<T>? where T: Comparable {
    guard lhs <= rhs else { return nil }
    return lhs ..< rhs
}

func isXLessThanOrEqualToM(x: String, m: Int, radix n: Int) -> Bool {
    var v = 0
    var k = 1
    var overflows = false
    for d in x.reversed() {
        if overflows { return false }
        guard let dv = Int(String(d))! *? k else { return false }
        guard let newV = v +? dv else { return false }
        v = newV
        if v > m { return false }
        if let newK = k *? n {
            k = newK
        } else {
            overflows = true
        }
    }
    return true
}

let x = readLine()!
let m = readInt1()

let d = Int(String(x.max()!))!

if x.count == 1 {
    if d > m {
        print(0)
    } else {
        print(1)
    }
} else {
    if let range = (d + 1) ..<? (m + 1) {
        let nn = range.values(<=) { n in isXLessThanOrEqualToM(x: x, m: m, radix: n) }
        print(nn.count)
    } else {
        print(0)
    }
}

提出情報

提出日時
問題 D - Base n
ユーザ koher
言語 Swift (5.2.1)
得点 400
コード長 2601 Byte
結果 AC
実行時間 12 ms
メモリ 7740 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 400 / 400
結果
AC × 3
AC × 47
セット名 テストケース
Sample sample_01.txt, sample_02.txt, sample_03.txt
All hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, one_digit_01.txt, one_digit_02.txt, one_digit_03.txt, one_digit_04.txt, one_digit_05.txt, one_digit_06.txt, one_digit_07.txt, one_digit_08.txt, one_digit_09.txt, one_digit_10.txt, one_digit_11.txt, one_digit_12.txt, one_digit_13.txt, one_digit_14.txt, one_digit_15.txt, one_digit_16.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, sample_01.txt, sample_02.txt, sample_03.txt
ケース名 結果 実行時間 メモリ
hand_01.txt AC 11 ms 7428 KiB
hand_02.txt AC 3 ms 7340 KiB
hand_03.txt AC 5 ms 7400 KiB
hand_04.txt AC 4 ms 7224 KiB
one_digit_01.txt AC 2 ms 7544 KiB
one_digit_02.txt AC 5 ms 7160 KiB
one_digit_03.txt AC 3 ms 7136 KiB
one_digit_04.txt AC 4 ms 7548 KiB
one_digit_05.txt AC 2 ms 7536 KiB
one_digit_06.txt AC 4 ms 7436 KiB
one_digit_07.txt AC 4 ms 7624 KiB
one_digit_08.txt AC 5 ms 7344 KiB
one_digit_09.txt AC 6 ms 7340 KiB
one_digit_10.txt AC 4 ms 7572 KiB
one_digit_11.txt AC 4 ms 7620 KiB
one_digit_12.txt AC 5 ms 7532 KiB
one_digit_13.txt AC 5 ms 7440 KiB
one_digit_14.txt AC 5 ms 7356 KiB
one_digit_15.txt AC 7 ms 7452 KiB
one_digit_16.txt AC 5 ms 7436 KiB
random_01.txt AC 6 ms 7604 KiB
random_02.txt AC 11 ms 7288 KiB
random_03.txt AC 4 ms 7572 KiB
random_04.txt AC 4 ms 7200 KiB
random_05.txt AC 5 ms 7340 KiB
random_06.txt AC 5 ms 7288 KiB
random_07.txt AC 5 ms 7480 KiB
random_08.txt AC 5 ms 7340 KiB
random_09.txt AC 6 ms 7368 KiB
random_10.txt AC 5 ms 7340 KiB
random_11.txt AC 7 ms 7480 KiB
random_12.txt AC 4 ms 7404 KiB
random_13.txt AC 5 ms 7420 KiB
random_14.txt AC 5 ms 7300 KiB
random_15.txt AC 8 ms 7740 KiB
random_16.txt AC 5 ms 7736 KiB
random_17.txt AC 6 ms 7612 KiB
random_18.txt AC 5 ms 7740 KiB
random_19.txt AC 6 ms 7464 KiB
random_20.txt AC 4 ms 7412 KiB
random_21.txt AC 5 ms 7296 KiB
random_22.txt AC 5 ms 7412 KiB
random_23.txt AC 12 ms 7580 KiB
random_24.txt AC 6 ms 7340 KiB
sample_01.txt AC 4 ms 7604 KiB
sample_02.txt AC 4 ms 7680 KiB
sample_03.txt AC 4 ms 7296 KiB