Submission #29600937
Source Code Expand
// D - Prime Sum Game
// https://atcoder.jp/contests/abc239/tasks/abc239_d
// 実行制限時間: 2.0 sec
import Foundation
func main() {
// =====================
// actual code goes here
// =====================
func getPrimes(upto n: Int) -> [Bool] {
var isPrime = [Bool](repeating: true, count: n + 1)
isPrime[0] = false
isPrime[1] = false
var x = 2
while x * x <= n {
defer { x += 1 }
guard isPrime[x] else { continue }
for y in stride(from: x + x, through: n, by: x) {
isPrime[y] = false
}
}
return isPrime
}
let (a, b, c, d) = readInts().tupled()
var tWinnable = false
let isPrime = getPrimes(upto: 200)
for takahashi in a...b {
if !(c...d).contains(where: { aoki in isPrime[aoki + takahashi]}) {
tWinnable = true
break
}
}
print(tWinnable ? "Takahashi" : "Aoki")
// ===============
// actual code end
// ===============
}
main()
func readString () -> String { readLine()! }
func readSubsequence () -> [String.SubSequence] { readString().split(separator: " ")}
func readChars () -> [Character] {readString().map({$0})}
func readStrings () -> [String] { readSubsequence().map({String($0)}) }
func readInt() -> Int { Int(readString())! }
func readInts() -> [Int] { readSubsequence().map{Int(String($0))!} } // TODO: remove the String conversion once Atcoder is updated to 5.5
func gcd(_ a: Int, _ b: Int) -> Int {
if b == 0 { return a }
return gcd(b, a%b)
}
func pf(_ numb: Int) -> [(prime: Int, count: Int)] {
var n = numb, i = 2, primeList = [(Int, Int)]()
while i * i <= numb && i <= n {
var count = 0
while n % i == 0 { n /= i; count += 1 }
if count > 0 { primeList.append((i, count)) }
i += 1
}
if n != 1 { primeList.append((n, 1)) }
return primeList
}
// reference https://ioritsutsui.com/permutation-full-enumeration/
func permutation<T>(_ args: [T]) -> [[T]] {
guard args.count > 1 else { return [args] }
func rotate(_ arr: [T]) -> [T] { return arr.dropFirst() + [arr.first!] }
var rotatedValue = args
var result = [[T]]()
for _ in 0..<args.count {
let head = rotatedValue.first!
let tails = Array(rotatedValue.dropFirst())
for arr in permutation(tails) {
result.append([head] + arr)
}
rotatedValue = rotate(rotatedValue)
}
return result
}
// based on: https://atcoder.jp/contests/abc235/submissions/28562251
struct Queue<T> {
private var data = [T](), pos=0
var isEmpty:Bool{ data.count==pos }
var front:T?{isEmpty ? nil : data[pos]}
mutating func push(_ t:T) { data.append(t) }
mutating func push(_ ts:[T]) { data.append(contentsOf: ts) }
mutating func pop()->T?{
if isEmpty { return nil }
pos += 1; return data[pos-1] }}
// based on: https://github.com/davecom/SwiftPriorityQueue/blob/7b4aa89d9740779f6123929c3e9e7e6b86b83671/Sources/SwiftPriorityQueue/SwiftPriorityQueue.swift
struct PriorityQueue<T> {
var heap = [T](); let order: (T, T) -> Bool
init(_ startingValues: ArraySlice<T> = [], order: @escaping (T, T) -> Bool) {
self.order = order; push(startingValues) }
init(_ startingValues: [T], order: @escaping (T, T) -> Bool) {
self.init(startingValues[...], order: order)}
var count: Int { heap.count }; var isEmpty: Bool { heap.isEmpty }
private mutating func sink(_ index: Int) {
var index = index
while 2 * index + 1 < count {
var j = 2 * index + 1
if j < (count - 1) && order(heap[j+1], heap[j]) { j += 1 }
guard order(heap[j], heap[index] ) else { break }
heap.swapAt(j, index); index = j } }
private mutating func swim(_ index: Int) {
var index = index
while index > 0 && order(heap[index], heap[(index - 1) / 2]) {
heap.swapAt(index, (index - 1) / 2)
index = (index - 1) / 2 } }
mutating func push(_ element: T) { heap.append(element); swim(count - 1) }
mutating func push(_ elements: ArraySlice<T>) { elements.forEach { push($0) } }
mutating func push(_ elements: [T]) { push(elements[...]) }
mutating func pop() -> T? {
guard !isEmpty else { return nil }
heap.swapAt(0, count - 1)
let first = heap.removeLast()
sink(0); return first } }
extension PriorityQueue where T: Comparable {
init(_ startingValues: ArraySlice<T> = [], smallerFirst: Bool = true) {
self.init(startingValues, order: smallerFirst ? {$0 < $1} : {$0 > $1}) }
init(_ startingValues: [T], smallerFirst: Bool = true) {
self.init(startingValues[...], smallerFirst: smallerFirst)} }
extension PriorityQueue: IteratorProtocol {
mutating func next() -> T? { return pop() }}
extension PriorityQueue: Sequence {
func makeIterator() -> PriorityQueue<T> { return self }}
extension PriorityQueue: Collection {
var startIndex: Int { return heap.startIndex }
var endIndex: Int { return heap.endIndex }
subscript(i: Int) -> T { return heap[i] }
func index(after i: Int) -> Int { return heap.index(after: i) }}
extension PriorityQueue: CustomStringConvertible, CustomDebugStringConvertible {
var description: String { return heap.description }
var debugDescription: String { return heap.debugDescription }}
// Based on: https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_1_A&lang=jp
struct DisjointSet {
private var rank: [Int]; private var p: [Int]
init(_ size: Int) {
rank = [Int](); p = [Int]()
for x in 0..<size { p.append(x); rank.append(0) }}
mutating func same(_ x: Int, _ y: Int) -> Bool { return findSet(x) == findSet(y) }
mutating func unite(_ x: Int, _ y: Int) { link(x, y) }
private mutating func findSet(_ x: Int) -> Int {
if x != p[x] { p[x] = findSet(p[x]) }
return p[x] }
private mutating func link(_ x: Int, _ y: Int) {
let a = findSet(x), b = findSet(y)
if rank[a] > rank[b] { p[b] = a
} else {
p[a] = b
if rank[a] == rank[b] { rank[b] += 1 } }}}
extension Bool {
/// Returns String "Yes" or "No" depending on the bool value
var yN: String { self ? "Yes" : "No" } }
extension Array {
func tupled() -> (Element, Element) { (self[0], self[1]) }
func tupled() -> (Element, Element, Element) { (self[0], self[1], self[2]) }
func tupled() -> (Element, Element, Element, Element) { (self[0], self[1],
self[2], self[3]) }
/// Returns a new string by concatenating the elements of the sequence,
/// adding the given separator between each element.
///
/// The following example shows how an array of Ints can be joined to a
/// single, comma-separated string:
///
/// let numbers = [1, 4, 2, 6]
/// let list = numbers.joinedAsString(separator: ", ")
/// print(list)
/// // Prints "1, 4, 2, 6"
///
/// - Parameter separator: A string to insert between each of the elements
/// in this sequence. The default separator is an empty string.
/// - Returns: A single, concatenated string.
func joinedAsString(separator: String = "") -> String {
self.map {"\($0)"}.joined(separator: separator) } }
Submission Info
| Submission Time |
|
| Task |
D - Prime Sum Game |
| User |
tockrock |
| Language |
Swift (5.2.1) |
| Score |
400 |
| Code Size |
7503 Byte |
| Status |
AC |
| Exec Time |
70 ms |
| Memory |
13176 KiB |
Judge Result
| Set Name |
Sample |
All |
| Score / Max Score |
0 / 0 |
400 / 400 |
| Status |
|
|
| Set Name |
Test Cases |
| Sample |
sample_01.txt, sample_02.txt, sample_03.txt |
| All |
hand_01.txt, hand_02.txt, hand_03.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, sample_01.txt, sample_02.txt, sample_03.txt |
| Case Name |
Status |
Exec Time |
Memory |
| hand_01.txt |
AC |
70 ms |
12528 KiB |
| hand_02.txt |
AC |
11 ms |
12828 KiB |
| hand_03.txt |
AC |
9 ms |
13176 KiB |
| random_01.txt |
AC |
10 ms |
12832 KiB |
| random_02.txt |
AC |
8 ms |
12792 KiB |
| random_03.txt |
AC |
8 ms |
13056 KiB |
| random_04.txt |
AC |
9 ms |
12664 KiB |
| random_05.txt |
AC |
13 ms |
13176 KiB |
| random_06.txt |
AC |
11 ms |
13176 KiB |
| random_07.txt |
AC |
9 ms |
12908 KiB |
| random_08.txt |
AC |
7 ms |
12584 KiB |
| random_09.txt |
AC |
9 ms |
12940 KiB |
| random_10.txt |
AC |
10 ms |
12828 KiB |
| random_11.txt |
AC |
8 ms |
13096 KiB |
| random_12.txt |
AC |
9 ms |
12848 KiB |
| random_13.txt |
AC |
7 ms |
12588 KiB |
| random_14.txt |
AC |
8 ms |
12912 KiB |
| random_15.txt |
AC |
8 ms |
12796 KiB |
| random_16.txt |
AC |
7 ms |
12860 KiB |
| random_17.txt |
AC |
8 ms |
12852 KiB |
| random_18.txt |
AC |
10 ms |
13036 KiB |
| random_19.txt |
AC |
8 ms |
12796 KiB |
| random_20.txt |
AC |
11 ms |
12848 KiB |
| random_21.txt |
AC |
7 ms |
12936 KiB |
| random_22.txt |
AC |
9 ms |
13092 KiB |
| random_23.txt |
AC |
8 ms |
12852 KiB |
| random_24.txt |
AC |
9 ms |
12796 KiB |
| random_25.txt |
AC |
9 ms |
13052 KiB |
| random_26.txt |
AC |
10 ms |
12944 KiB |
| random_27.txt |
AC |
8 ms |
12660 KiB |
| sample_01.txt |
AC |
9 ms |
12584 KiB |
| sample_02.txt |
AC |
10 ms |
12936 KiB |
| sample_03.txt |
AC |
9 ms |
12940 KiB |