結果
問題 | No.1260 たくさんの多項式 |
ユーザー |
![]() |
提出日時 | 2020-10-16 22:56:27 |
言語 | Swift (6.0.3) |
結果 |
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 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 61 |
ソースコード
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)