Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- protocol Stackable {
- associatedtype Element
- func peek() -> Element?
- mutating func push(_ element: Element)
- @discardableResult mutating func pop() -> Element?
- }
- extension Stackable {
- var isEmpty: Bool { peek() == nil }
- }
- struct Stack<Element>: Stackable where Element: Equatable {
- private var storage = [Element]()
- func peek() -> Element? { storage.last }
- mutating func push(_ element: Element) { storage.append(element) }
- mutating func pop() -> Element? { storage.popLast() }
- }
- extension Stack: Equatable {
- static func == (lhs: Stack<Element>, rhs: Stack<Element>) -> Bool { lhs.storage == rhs.storage }
- }
- extension Stack: CustomStringConvertible {
- var description: String { "\(storage)" }
- }
- extension Stack: ExpressibleByArrayLiteral {
- init(arrayLiteral elements: Self.Element...) { storage = elements }
- }
- let instructions = readLine()!.split(separator: " ").map { Int64($0)! }
- let n = instructions[0]
- let a = instructions[1]
- let b = instructions[2]
- let c = instructions[3]
- let x0 = instructions[4]
- var stack = Stack<Int64>()
- func getNumber(previousX: Int64) -> Int64 {
- return ((a * previousX * previousX + b * previousX + c) / 100) % 1_000_000
- }
- var prev: Int64 = x0
- var sum: Int64 = 0
- for _ in 1...1_000_000 {
- prev = getNumber(previousX: prev)
- if prev % 5 < 2 {
- if !stack.isEmpty {
- stack.pop()
- }
- } else {
- if let last = stack.peek() {
- stack.push(min(prev, last))
- } else {
- stack.push(prev)
- }
- }
- if let last = stack.peek() {
- sum += last
- }
- }
- print(sum)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement