結果

問題 No.1260 たくさんの多項式
ユーザー semisagisemisagi
提出日時 2020-10-16 22:56:27
言語 Swift
(5.10.0)
結果
AC  
実行時間 534 ms / 2,000 ms
コード長 3,233 bytes
コンパイル時間 1,490 ms
コンパイル使用メモリ 130,464 KB
実行使用メモリ 8,040 KB
最終ジャッジ日時 2023-09-28 04:50:14
合計ジャッジ時間 15,469 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
7,824 KB
testcase_01 AC 14 ms
7,960 KB
testcase_02 AC 14 ms
7,964 KB
testcase_03 AC 14 ms
7,832 KB
testcase_04 AC 13 ms
7,964 KB
testcase_05 AC 14 ms
7,896 KB
testcase_06 AC 14 ms
7,968 KB
testcase_07 AC 14 ms
7,948 KB
testcase_08 AC 14 ms
8,004 KB
testcase_09 AC 14 ms
7,936 KB
testcase_10 AC 14 ms
7,984 KB
testcase_11 AC 13 ms
7,900 KB
testcase_12 AC 14 ms
7,960 KB
testcase_13 AC 14 ms
7,828 KB
testcase_14 AC 14 ms
7,828 KB
testcase_15 AC 14 ms
8,024 KB
testcase_16 AC 13 ms
7,948 KB
testcase_17 AC 14 ms
7,900 KB
testcase_18 AC 14 ms
7,836 KB
testcase_19 AC 14 ms
7,940 KB
testcase_20 AC 515 ms
7,836 KB
testcase_21 AC 60 ms
7,956 KB
testcase_22 AC 392 ms
7,960 KB
testcase_23 AC 270 ms
7,900 KB
testcase_24 AC 287 ms
7,832 KB
testcase_25 AC 183 ms
7,956 KB
testcase_26 AC 257 ms
8,004 KB
testcase_27 AC 169 ms
7,960 KB
testcase_28 AC 490 ms
7,944 KB
testcase_29 AC 275 ms
7,968 KB
testcase_30 AC 121 ms
7,944 KB
testcase_31 AC 280 ms
7,960 KB
testcase_32 AC 277 ms
7,948 KB
testcase_33 AC 190 ms
7,840 KB
testcase_34 AC 456 ms
7,896 KB
testcase_35 AC 108 ms
7,832 KB
testcase_36 AC 92 ms
7,996 KB
testcase_37 AC 286 ms
7,964 KB
testcase_38 AC 330 ms
7,992 KB
testcase_39 AC 470 ms
7,948 KB
testcase_40 AC 277 ms
7,940 KB
testcase_41 AC 417 ms
7,932 KB
testcase_42 AC 239 ms
7,904 KB
testcase_43 AC 453 ms
7,836 KB
testcase_44 AC 201 ms
7,948 KB
testcase_45 AC 183 ms
7,900 KB
testcase_46 AC 157 ms
8,040 KB
testcase_47 AC 233 ms
7,904 KB
testcase_48 AC 31 ms
7,964 KB
testcase_49 AC 140 ms
7,964 KB
testcase_50 AC 53 ms
7,952 KB
testcase_51 AC 461 ms
7,948 KB
testcase_52 AC 70 ms
7,936 KB
testcase_53 AC 112 ms
7,900 KB
testcase_54 AC 487 ms
7,948 KB
testcase_55 AC 524 ms
8,036 KB
testcase_56 AC 125 ms
7,936 KB
testcase_57 AC 249 ms
7,940 KB
testcase_58 AC 107 ms
7,952 KB
testcase_59 AC 274 ms
8,036 KB
testcase_60 AC 534 ms
7,952 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