結果

問題 No.1097 Remainder Operation
ユーザー semisagisemisagi
提出日時 2020-06-26 21:47:29
言語 Swift
(5.10.0)
結果
AC  
実行時間 314 ms / 2,000 ms
コード長 1,518 bytes
コンパイル時間 1,081 ms
コンパイル使用メモリ 128,764 KB
実行使用メモリ 17,184 KB
最終ジャッジ日時 2024-05-07 22:10:19
合計ジャッジ時間 6,434 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
9,216 KB
testcase_01 AC 9 ms
9,216 KB
testcase_02 AC 8 ms
9,216 KB
testcase_03 AC 9 ms
9,216 KB
testcase_04 AC 8 ms
9,216 KB
testcase_05 AC 8 ms
9,216 KB
testcase_06 AC 9 ms
9,216 KB
testcase_07 AC 35 ms
9,904 KB
testcase_08 AC 35 ms
9,904 KB
testcase_09 AC 35 ms
9,904 KB
testcase_10 AC 35 ms
9,900 KB
testcase_11 AC 35 ms
9,900 KB
testcase_12 AC 310 ms
16,192 KB
testcase_13 AC 311 ms
16,160 KB
testcase_14 AC 302 ms
16,284 KB
testcase_15 AC 308 ms
16,156 KB
testcase_16 AC 305 ms
16,160 KB
testcase_17 AC 314 ms
16,160 KB
testcase_18 AC 269 ms
16,928 KB
testcase_19 AC 282 ms
17,136 KB
testcase_20 AC 270 ms
16,924 KB
testcase_21 AC 267 ms
16,924 KB
testcase_22 AC 265 ms
17,184 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Scanner {
    private var stack = [String]()
    private var index = 0
    
    func peek() -> String {
        if stack.count == index {
            stack += readLine()!.split(separator: " ").map{ String($0) }
        }
        return stack[index]
    }
    
    func next() -> String {
        let value = peek()
        index += 1
        return value
    }
    
    func nextInt() -> Int {
        return Int(next())!
    }
    
    func nextDouble() -> Double {
        return Double(next())!
    }
}

let scanner = Scanner()
let N = scanner.nextInt()
let A = (0 ..< N).map { _ in scanner.nextInt() }
let Q = scanner.nextInt()

var time = [Int?](repeating: nil, count: N)

var index = 0
var now = 0
var footprints = [Int]()
while time[index] == nil {
    footprints.append(index)
    time[index] = now
    now += 1
    index += A[index]
    index %= N
}
let periodStart = time[index]!
let periodLength = now - time[index]!

var prefixSum = [Int](repeating: 0, count: footprints.count + 1)
for i in 0 ..< footprints.count {
    prefixSum[i + 1] = prefixSum[i] + A[footprints[i]]
}

for _ in 0 ..< Q {
    var K = scanner.nextInt()
    if K < periodStart {
        print(prefixSum[K])
    } else {
        var answer = 0
        answer += prefixSum[periodStart]
        K -= periodStart
        answer += (prefixSum[footprints.count] - prefixSum[periodStart]) * (K / periodLength)
        K %= periodLength
        answer += (prefixSum[periodStart + K] - prefixSum[periodStart])
        print(answer)
    }
}
0