66 lines
1.5 KiB
Swift
66 lines
1.5 KiB
Swift
if let T = Int(readLine() ?? ""),
|
|
let n = Int(readLine() ?? ""),
|
|
let inputA = readLine(),
|
|
let A = inputA.split(separator: " ").compactMap({Int($0)}) as? [Int],
|
|
let m = Int(readLine() ?? ""),
|
|
let inputB = readLine(),
|
|
let B = inputB.split(separator: " ").compactMap({Int($0)}) as? [Int],
|
|
A.count == n , B.count == m
|
|
{
|
|
var sumA: [Int] = []
|
|
for i in 0..<n {
|
|
var currentSumA = 0
|
|
for j in i..<n {
|
|
currentSumA += A[j]
|
|
sumA.append(currentSumA)
|
|
}
|
|
}
|
|
|
|
sumA.sort()
|
|
|
|
var sumB: [Int] = []
|
|
for i in 0..<m {
|
|
var currentSumB = 0
|
|
for j in i..<m {
|
|
currentSumB += B[j]
|
|
sumB.append(currentSumB)
|
|
}
|
|
}
|
|
|
|
sumB.sort()
|
|
|
|
var left = 0
|
|
var right = sumB.count-1
|
|
var ans = 0
|
|
|
|
while left < sumA.count && right >= 0 {
|
|
let temp = sumA[left] + sumB[right]
|
|
|
|
if T > temp {
|
|
left += 1
|
|
}
|
|
else if T < temp {
|
|
right -= 1
|
|
}
|
|
else {
|
|
var sameCountA = 0
|
|
let currentSumA = sumA[left]
|
|
while left < sumA.count && sumA[left] == currentSumA {
|
|
sameCountA += 1
|
|
left += 1
|
|
}
|
|
|
|
var sameCountB = 0
|
|
let currentSumB = sumB[right]
|
|
while right >= 0 && sumB[right] == currentSumB {
|
|
sameCountB += 1
|
|
right -= 1
|
|
}
|
|
|
|
ans += sameCountA * sameCountB
|
|
}
|
|
}
|
|
|
|
print(ans)
|
|
}
|