๐Ÿ™‡โ€โ™€๏ธ[Gold I] ๋ƒ…์ƒ‰๋ฌธ์ œ - 1450

๋ฌธ์ œ ๋งํฌ

์„ฑ๋Šฅ ์š”์•ฝ

๋ฉ”๋ชจ๋ฆฌ: 2920 KB, ์‹œ๊ฐ„: 8 ms

๋ถ„๋ฅ˜

์ด๋ถ„ ํƒ์ƒ‰, ์ค‘๊ฐ„์—์„œ ๋งŒ๋‚˜๊ธฐ

์ œ์ถœ ์ผ์ž

2024๋…„ 6์›” 10์ผ 20:15:43

๋ฌธ์ œ ์„ค๋ช…

์„ธ์ค€์ด๋Š” N๊ฐœ์˜ ๋ฌผ๊ฑด์„ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ , ์ตœ๋Œ€ C๋งŒํผ์˜ ๋ฌด๊ฒŒ๋ฅผ ๋„ฃ์„ ์ˆ˜ ์žˆ๋Š” ๊ฐ€๋ฐฉ์„ ํ•˜๋‚˜ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

N๊ฐœ์˜ ๋ฌผ๊ฑด์„ ๊ฐ€๋ฐฉ์— ๋„ฃ๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— N๊ณผ C๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. N์€ 30๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜, C๋Š” 109๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์Œ์ด ์•„๋‹Œ ์ •์ˆ˜์ด๋‹ค. ๋‘˜์งธ ์ค„์— ๋ฌผ๊ฑด์˜ ๋ฌด๊ฒŒ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋ฌด๊ฒŒ๋„ 109๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ๊ฐ€๋ฐฉ์— ๋„ฃ๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿš€ํ’€์ด

์ด ๋ฌธ์ œ๋Š” ๋ถ€๋ถ„ ์ง‘ํ•ฉ์„ ํ™œ์šฉํ•œ ์กฐํ•ฉ์˜ ๋ฌธ์ œ๋กœ, ์ฃผ์–ด์ง„ ๋ฌผ๊ฑด๋“ค์˜ ๋ฌด๊ฒŒ๋ฅผ ์กฐํ•ฉํ•˜์—ฌ ์ตœ๋Œ€ ๋ฌด๊ฒŒ C๋ฅผ ์ดˆ๊ณผํ•˜์ง€ ์•Š๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.
๋ฌผ๊ฑด์˜ ๊ฐœ์ˆ˜๊ฐ€ 30๊ฐœ ์ดํ•˜์ด๊ธฐ ๋•Œ๋ฌธ์—, ์ด๋ฅผ ํšจ์œจ์ ์œผ๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜๋Š” โ€œMeet in the Middleโ€ ๊ธฐ๋ฒ•์ž…๋‹ˆ๋‹ค.

  • ํ•ด๊ฒฐ ์ „๋žต
    • ์ฃผ์–ด์ง„ ๋ฌผ๊ฑด์„ ๋‘ ๊ทธ๋ฃน์œผ๋กœ ๋‚˜๋ˆ•๋‹ˆ๋‹ค.
    • ๊ฐ๊ฐ์˜ ๊ทธ๋ฃน์—์„œ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๋ถ€๋ถ„ ์ง‘ํ•ฉ์˜ ํ•ฉ์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.
    • ๋‘ ๊ทธ๋ฃน์˜ ํ•ฉ์„ ์กฐํ•ฉํ•˜์—ฌ ์ตœ๋Œ€ ๋ฌด๊ฒŒ C๋ฅผ ์ดˆ๊ณผํ•˜์ง€ ์•Š๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.
  • ๊ตฌํ˜„ ๋‹จ๊ณ„
    • ๋ฌผ๊ฑด์„ ๋‘ ๊ทธ๋ฃน์œผ๋กœ ๋‚˜๋ˆˆ ํ›„, ๊ฐ ๊ทธ๋ฃน์—์„œ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๋ถ€๋ถ„ ์ง‘ํ•ฉ์˜ ํ•ฉ์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค.
    • ํ•œ ๊ทธ๋ฃน์˜ ํ•ฉ์„ ๊ณ ์ •ํ•˜๊ณ , ๋‹ค๋ฅธ ๊ทธ๋ฃน์˜ ํ•ฉ์—์„œ ์ ์ ˆํ•œ ๊ฐ’์„ ์ด์ง„ ํƒ์ƒ‰์œผ๋กœ ์ฐพ์•„ ์กฐํ•ฉํ•˜์—ฌ ์ตœ๋Œ€ ๋ฌด๊ฒŒ C๋ฅผ ์ดˆ๊ณผํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.
  • ์ž…๋ ฅ ์ฒ˜๋ฆฌ: N๊ณผ C, ๊ทธ๋ฆฌ๊ณ  ๋ฌผ๊ฑด์˜ ๋ฌด๊ฒŒ๋ฅผ ์ž…๋ ฅ๋ฐ›์Šต๋‹ˆ๋‹ค.
  • ๋ถ€๋ถ„ ์ง‘ํ•ฉ ํ•ฉ ๊ณ„์‚ฐ: calculateSubsets ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ๋‘ ๊ทธ๋ฃน์œผ๋กœ ๋‚˜๋ˆˆ ๊ฐ๊ฐ์˜ ๋ฌผ๊ฑด์—์„œ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๋ถ€๋ถ„ ์ง‘ํ•ฉ์˜ ํ•ฉ์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.
  • ์ด์ง„ ํƒ์ƒ‰์„ ํ†ตํ•œ ์กฐํ•ฉ ๊ณ„์‚ฐ: ์ฒซ ๋ฒˆ์งธ ๊ทธ๋ฃน์˜ ํ•ฉ์„ ๊ณ ์ •ํ•˜๊ณ , ๋‘ ๋ฒˆ์งธ ๊ทธ๋ฃน์˜ ํ•ฉ์—์„œ C๋ฅผ ๋„˜์ง€ ์•Š๋Š” ๊ฐ’์„ ์ด์ง„ ํƒ์ƒ‰์„ ํ†ตํ•ด ์ฐพ์•„์„œ ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ์กฐํ•ฉ์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.
  • ๊ฒฐ๊ณผ ์ถœ๋ ฅ: ๊ณ„์‚ฐ๋œ ๋ฐฉ๋ฒ•์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

๋ชฐ๋ผ์„œ ์ฐพ์•„๋ณธ๊ฑด๋ฐ ๋ด๋„ ๋ชจ๋ฅด๊ฒ ๋‹ค.

Meet in the Middle์— ๋Œ€ํ•ด์„œ ๋” ์ฐพ์•„๋ดค๋‹ค.

Meet in the Middle ๊ธฐ๋ฒ• ์„ค๋ช…

๊ฐœ๋…

  1. ๋ฌธ์ œ ๋ถ„ํ• : ์ฃผ์–ด์ง„ ๋ฌธ์ œ๋ฅผ ๋‘ ๊ฐœ์˜ ํ•˜์œ„ ๋ฌธ์ œ๋กœ ๋ถ„ํ• ํ•ฉ๋‹ˆ๋‹ค.
  2. ๋ถ€๋ถ„ ์ง‘ํ•ฉ ๊ณ„์‚ฐ: ๊ฐ ํ•˜์œ„ ๋ฌธ์ œ์—์„œ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๋ถ€๋ถ„ ์ง‘ํ•ฉ์˜ ๊ฐ’์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.
  3. ์กฐํ•ฉ: ๋‘ ํ•˜์œ„ ๋ฌธ์ œ์—์„œ ์–ป์€ ๋ถ€๋ถ„ ์ง‘ํ•ฉ์„ ์ ์ ˆํžˆ ์กฐํ•ฉํ•˜์—ฌ ์ตœ์ข… ๋ฌธ์ œ์˜ ํ•ด๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์€ ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฌธ์ œ์˜ ํฌ๊ธฐ๋ฅผ ์ ˆ๋ฐ˜์œผ๋กœ ์ค„์ด๊ณ , ๊ฐ ์ ˆ๋ฐ˜์— ๋Œ€ํ•ด ๋ชจ๋“  ๋ถ€๋ถ„ ์ง‘ํ•ฉ์„ ๊ณ„์‚ฐํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์›๋ž˜ ๋ฌธ์ œ๊ฐ€ ์ง€๋‹Œ ํƒ์ƒ‰ ๊ณต๊ฐ„์˜ ํฌ๊ธฐ๋ฅผ ํฌ๊ฒŒ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ผ๊ณ  ํ•œ๋‹ค. ์•„ ๋ชฐ๋ผ

๐Ÿš€์ฝ”๋“œ

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include<iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <climits>

using namespace std;

// ์ฃผ์–ด์ง„ ๋ฒกํ„ฐ์—์„œ ๊ฐ€๋Šฅํ•œ ๋ถ€๋ถ„ ์ง‘ํ•ฉ์˜ ํ•ฉ์„ ๋ชจ๋‘ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜
void calculateSubsets(const vector<long long>& weights, vector<long long>& subsets) 
{
    int n = weights.size();
    for (int i = 0; i < (1 << n); ++i) 
    {
        long long sum = 0;
        for (int j = 0; j < n; ++j) 
        {
            if (i & (1 << j)) 
            {
                sum += weights[j];
            }
        }
        subsets.push_back(sum);
    }
}

void solve()
{
    int N;
    long long C;
    cin >> N >> C;

    vector<long long> weights(N);
    for (int i = 0; i < N; ++i) 
    {
        cin >> weights[i];
    }

    // ๋ฌผ๊ฑด์„ ๋‘ ๊ทธ๋ฃน์œผ๋กœ ๋‚˜๋ˆ„๊ธฐ
    int mid = N / 2;
    vector<long long> group1(weights.begin(), weights.begin() + mid);
    vector<long long> group2(weights.begin() + mid, weights.end());

    vector<long long> subsets1, subsets2;

    // ๊ฐ ๊ทธ๋ฃน์—์„œ ๊ฐ€๋Šฅํ•œ ๋ถ€๋ถ„ ์ง‘ํ•ฉ์˜ ํ•ฉ ๊ณ„์‚ฐ
    calculateSubsets(group1, subsets1);
    calculateSubsets(group2, subsets2);

    // ๋‘ ๋ฒˆ์งธ ๊ทธ๋ฃน์˜ ๋ถ€๋ถ„ ์ง‘ํ•ฉ ํ•ฉ์„ ์ •๋ ฌ
    sort(subsets2.begin(), subsets2.end());

    long long count = 0;

    // ์ฒซ ๋ฒˆ์งธ ๊ทธ๋ฃน์˜ ๋ถ€๋ถ„ ์ง‘ํ•ฉ ํ•ฉ์„ ์ˆœํšŒํ•˜๋ฉฐ ๊ฐ€๋Šฅํ•œ ์กฐํ•ฉ์„ ์ฐพ๊ธฐ
    for (const auto& sum1 : subsets1) 
    {
        if (sum1 <= C) 
        {
            // subsets2์—์„œ sum1๊ณผ ํ•ฉ์ณ์„œ C๋ฅผ ๋„˜์ง€ ์•Š๋Š” ์š”์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ฐพ๊ธฐ
            count += upper_bound(subsets2.begin(), subsets2.end(), C - sum1) - subsets2.begin();
        }
    }

    cout << count << '\n';
}

int main()
{
	FILE* stream;
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	freopen_s(&stream, "input.txt", "rt", stdin);

	solve();

	return 0;
}

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ: