結果
問題 | No.1336 Union on Blackboard |
ユーザー |
![]() |
提出日時 | 2021-01-15 22:09:30 |
言語 | Swift (6.0.3) |
結果 |
AC
|
実行時間 | 15 ms / 2,000 ms |
コード長 | 3,222 bytes |
コンパイル時間 | 7,857 ms |
コンパイル使用メモリ | 181,320 KB |
実行使用メモリ | 16,000 KB |
最終ジャッジ日時 | 2024-11-26 15:07:27 |
合計ジャッジ時間 | 5,136 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 31 |
ソースコード
import Foundationstruct Scanner {private var elements = [String]()private var index = 0mutating func peek() -> String {while elements.count == index {elements = readLine()!.split(separator: " ").map(String.init)index = 0}return elements[index]}mutating func next() -> String {defer { index += 1 }return peek()}mutating func nextInt() -> Int {return Int(next())!}mutating func nextInts(_ n: Int) -> [Int] {return (0 ..< n).map { _ in nextInt() }}mutating func nextDouble() -> Double {return Double(next())!}}let mod = 1000000007struct Zn: CustomStringConvertible, AdditiveArithmetic {let n: Intinit(_ 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.nreturn 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.nreturn 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: Zn, rhs: Zn) -> Self {return lhs * rhs.inverse}static func / (lhs: Int, rhs: Zn) -> Self {return Zn(lhs) * rhs.inverse}static func / (lhs: Zn, rhs: Int) -> Self {return lhs * Zn(rhs).inverse}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)}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 T = scanner.nextInt()for _ in 0 ..< T {let N = scanner.nextInt()let A = scanner.nextInts(N)print(A.map(Zn.init).map { $0 + Zn(1) }.reduce(.one, *) - Zn(1))}