結果

問題 No.1260 たくさんの多項式
ユーザー semisagisemisagi
提出日時 2020-10-16 22:52:07
言語 Swift
(5.10.0)
結果
WA  
実行時間 -
コード長 3,217 bytes
コンパイル時間 7,232 ms
コンパイル使用メモリ 131,088 KB
実行使用メモリ 8,032 KB
最終ジャッジ日時 2023-09-28 04:40:08
合計ジャッジ時間 19,349 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
8,004 KB
testcase_01 AC 12 ms
7,952 KB
testcase_02 AC 12 ms
7,748 KB
testcase_03 AC 13 ms
8,028 KB
testcase_04 AC 12 ms
7,820 KB
testcase_05 AC 12 ms
7,940 KB
testcase_06 AC 12 ms
7,936 KB
testcase_07 AC 12 ms
7,884 KB
testcase_08 AC 12 ms
7,940 KB
testcase_09 AC 12 ms
7,932 KB
testcase_10 AC 12 ms
7,956 KB
testcase_11 AC 13 ms
7,960 KB
testcase_12 AC 13 ms
7,836 KB
testcase_13 AC 12 ms
7,880 KB
testcase_14 AC 13 ms
7,896 KB
testcase_15 AC 12 ms
7,812 KB
testcase_16 AC 12 ms
7,752 KB
testcase_17 AC 13 ms
7,936 KB
testcase_18 AC 13 ms
7,832 KB
testcase_19 AC 13 ms
7,940 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 AC 27 ms
7,880 KB
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 WA -
権限があれば一括ダウンロードができます

ソースコード

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) * (N % MOD)
    answer += Zn(r - k) * (N / k % MOD)
    answer -= sum1(l: k, r: r) * (N / k % MOD)
    k = r
}

print(answer)

0