BOJ 2143, 두 배열의 합

lilamaris
March 11, 2026 2 min read

문제

한 배열 A[1], A[2], …, A[n]에 대해서, 부 배열은 A[i], A[i+1], …, A[j-1], A[j] (단, 1 ≤ i ≤ j ≤ n)을 말한다. 이러한 부 배열의 합은 A[i]+…+A[j]를 의미한다. 각 원소가 정수인 두 배열 A[1], …, A[n]과 B[1], …, B[m]이 주어졌을 때, A의 부 배열의 합에 B의 부 배열의 합을 더해서 T가 되는 모든 부 배열 쌍의 개수를 구하는 프로그램을 작성하시오.

예를 들어 A = {1, 3, 1, 2}, B = {1, 3, 2}, T=5인 경우, 부 배열 쌍의 개수는 다음의 7가지 경우가 있다.

T(=5) = A[1] + B[1] + B[2] = A[1] + A[2] + B[1] = A[2] + B[3] = A[2] + A[3] + B[1] = A[3] + B[1] + B[2] = A[3] + A[4] + B[3] = A[4] + B[2]

입력

첫째 줄에 T(-1,000,000,000 ≤ T ≤ 1,000,000,000)가 주어진다. 다음 줄에는 n(1 ≤ n ≤ 1,000)이 주어지고, 그 다음 줄에 n개의 정수로 A[1], …, A[n]이 주어진다. 다음 줄에는 m(1 ≤ m ≤ 1,000)이 주어지고, 그 다음 줄에 m개의 정수로 B[1], …, B[m]이 주어진다. 각각의 배열 원소는 절댓값이 1,000,000을 넘지 않는 정수이다.

출력

첫째 줄에 답을 출력한다. 가능한 경우가 한 가지도 없을 경우에는 0을 출력한다.

예제 입력 1

5
4
1 3 1 2
3
1 3 2

예제 출력 1

7

내 구현

#include <algorithm>
#include <iostream>
#include <vector>
#define IO std::cin.tie(NULL), std::ios_base::sync_with_stdio(false)

#define MAX 1001

int A[MAX], B[MAX];
std::vector<int> PA, PB;
int T, N, M;

void solve() {
  std::cin >> T >> N;

  for (int i = 0; i < N; i++) {
    std::cin >> A[i];
  }

  std::cin >> M;

  for (int i = 0; i < M; i++) {
    std::cin >> B[i];
  }

  for (int i = 0; i < N; i++) {
    PA.push_back(A[i]);
    for (int j = i + 1; j < N; j++) {
      PA.push_back(A[j] + PA.back());
    }
  }

  for (int i = 0; i < M; i++) {
    PB.push_back(B[i]);
    for (int j = i + 1; j < M; j++) {
      PB.push_back(B[j] + PB.back());
    }
  }

  std::sort(PB.begin(), PB.end());

  long long count = 0;

  for (auto Ae : PA) {
    int target = T - Ae;

    int lo = std::lower_bound(PB.begin(), PB.end(), target) - PB.begin();

    int hi = std::upper_bound(PB.begin(), PB.end(), target) - PB.begin();

    count += (hi - lo);
  }

  std::cout << count;
}

int main() {
  IO;
  solve();
  return 0;
}

© 2025 All Rights Reserved. Made with 🤍 by lilamaris