結果

問題 No.1260 たくさんの多項式
ユーザー semisagisemisagi
提出日時 2020-10-16 22:56:27
言語 Swift
(5.10.0)
結果
AC  
実行時間 1,150 ms / 2,000 ms
コード長 3,233 bytes
コンパイル時間 11,574 ms
コンパイル使用メモリ 129,576 KB
実行使用メモリ 9,344 KB
最終ジャッジ日時 2024-07-20 23:24:01
合計ジャッジ時間 32,606 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
9,344 KB
testcase_01 AC 19 ms
9,216 KB
testcase_02 AC 18 ms
9,216 KB
testcase_03 AC 18 ms
9,088 KB
testcase_04 AC 18 ms
9,088 KB
testcase_05 AC 18 ms
9,088 KB
testcase_06 AC 18 ms
9,216 KB
testcase_07 AC 18 ms
9,088 KB
testcase_08 AC 18 ms
9,216 KB
testcase_09 AC 18 ms
9,216 KB
testcase_10 AC 19 ms
9,216 KB
testcase_11 AC 18 ms
9,216 KB
testcase_12 AC 19 ms
9,216 KB
testcase_13 AC 18 ms
9,088 KB
testcase_14 AC 19 ms
9,216 KB
testcase_15 AC 18 ms
9,216 KB
testcase_16 AC 19 ms
9,088 KB
testcase_17 AC 19 ms
9,088 KB
testcase_18 AC 19 ms
9,216 KB
testcase_19 AC 18 ms
9,216 KB
testcase_20 AC 1,128 ms
9,088 KB
testcase_21 AC 110 ms
9,088 KB
testcase_22 AC 824 ms
9,216 KB
testcase_23 AC 563 ms
9,216 KB
testcase_24 AC 596 ms
9,088 KB
testcase_25 AC 382 ms
9,216 KB
testcase_26 AC 551 ms
9,216 KB
testcase_27 AC 359 ms
9,216 KB
testcase_28 AC 1,039 ms
9,344 KB
testcase_29 AC 576 ms
9,216 KB
testcase_30 AC 249 ms
9,216 KB
testcase_31 AC 588 ms
9,088 KB
testcase_32 AC 600 ms
9,216 KB
testcase_33 AC 395 ms
9,216 KB
testcase_34 AC 976 ms
9,088 KB
testcase_35 AC 215 ms
9,216 KB
testcase_36 AC 182 ms
9,216 KB
testcase_37 AC 601 ms
9,216 KB
testcase_38 AC 708 ms
9,344 KB
testcase_39 AC 1,018 ms
9,216 KB
testcase_40 AC 582 ms
9,344 KB
testcase_41 AC 895 ms
9,216 KB
testcase_42 AC 488 ms
9,216 KB
testcase_43 AC 953 ms
9,088 KB
testcase_44 AC 412 ms
9,216 KB
testcase_45 AC 375 ms
9,088 KB
testcase_46 AC 322 ms
9,088 KB
testcase_47 AC 483 ms
9,216 KB
testcase_48 AC 45 ms
9,216 KB
testcase_49 AC 288 ms
9,216 KB
testcase_50 AC 95 ms
9,088 KB
testcase_51 AC 991 ms
9,216 KB
testcase_52 AC 133 ms
9,216 KB
testcase_53 AC 227 ms
9,216 KB
testcase_54 AC 1,049 ms
9,216 KB
testcase_55 AC 1,123 ms
9,216 KB
testcase_56 AC 264 ms
9,088 KB
testcase_57 AC 524 ms
9,088 KB
testcase_58 AC 216 ms
9,216 KB
testcase_59 AC 581 ms
9,344 KB
testcase_60 AC 1,150 ms
9,216 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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 {
        let value = peek()
        index += 1
        return value
    }

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

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

let MOD = 1000000007

struct Zn: CustomStringConvertible {
    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: 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)
    }
}

var scanner = Scanner()
let N = scanner.nextInt()

func f(n: Int, x: Int) -> Zn {
    var n = n
    var result = 0
    while n > 0 {
        result += n % x
        n /= x
    }
    return Zn(result % MOD)
}

func sum1(l: Int, r: Int) -> Zn {
    Zn((r - l) % MOD) * Zn((l + r - 1) % MOD) * Zn(2).inverse
}

var answer = Zn.zero
for i in 2 ... min(N, 1000000) {
    answer += f(n: N, x: i)
}

var k = 1000001
while k <= N {
    var l = k
    var r = N + 1
    while r - l > 1 {
        let mid = (l + r) / 2
        if N / k != N / mid {
            r = mid
        } else {
            l = mid
        }
    }
    answer += Zn((r - k) % MOD) * (N % MOD)
    answer += Zn((r - k) % MOD) * (N / k % MOD)
    answer -= sum1(l: k, r: r) * (N / k % MOD)
    k = r
}

print(answer)

0