Cod sursa(job #3238371)

Utilizator M132M132 M132 M132 Data 24 iulie 2024 18:49:23
Problema Submultimi Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.83 kb
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;

const int VECTOR_LENGTH = 10;
int NUM_VECTORS;

bool findAllSubsets(const vector<vector<int>>& vectors, const vector<int>& target, vector<vector<int>>& choices) {
    vector<int> sum(VECTOR_LENGTH, 0);
    bool foundAnySubset = false;

    // Iterate over all possible subsets
    for (int subset = 1; subset < (1 << NUM_VECTORS); ++subset) {
        std::fill(sum.begin(), sum.end(), 0);
        vector<int> subsetIndices;
        
        // Calculate the sum of the current subset
        for (int i = 0; i < NUM_VECTORS; ++i) {
            if (subset & (1 << i)) {
                subsetIndices.push_back(i);
                for (int j = 0; j < VECTOR_LENGTH; ++j) {
                    sum[j] += vectors[i][j];
                    // if (sum[j] > target[j]) break; how to break out?
                }
            }
        }
        
        // Check if this sum matches the target
        if (sum == target) {
            foundAnySubset = true;
            choices.push_back(subsetIndices);
        }
    }

    return foundAnySubset;
}

vector<int> countDigits(int n) {
    vector<int> digitCount(10, 0);

    // Iterate over each digit position (units, tens, hundreds, etc.)
    for (int pos = 1; pos <= n; pos *= 10) {
        int higher = n / (pos * 10);
        int lower = n % pos;
        int currentDigit = (n / pos) % 10;
        
        // Count occurrences of digits in the current digit position
        for (int digit = 0; digit < 10; ++digit) {
            if (digit < currentDigit) {
                digitCount[digit] += (higher + 1) * pos;
            } else if (digit == currentDigit) {
                digitCount[digit] += higher * pos + lower + 1;
            } else {
                digitCount[digit] += higher * pos;
            }
        }

        // Special case handling for digit 0 to avoid counting leading zeros
        if (pos > 1) {
            digitCount[0] -= pos;
        }
    }

    return digitCount;
}

int main()
{
    ifstream inputFile("colectie.in");
    int N, K; inputFile >> N >> K;
    vector<vector<int>> v;
    for (int i = 0; i < N; i++) {
        vector<int> tmp(10);
        for (int j = 0; j < 10; j++) inputFile >> tmp[j];
        v.push_back(tmp);
    }

    NUM_VECTORS = N;
    vector<int> target = countDigits(K);
    target[0]--;
    vector<vector<int>> choices;
    bool found = findAllSubsets(v, target, choices);

    ofstream outputFile("colectie.out");
    if (found) {
        outputFile << "1\n";
        vector<int> minBoxes = choices[0];
        for (vector<int> tmp: choices) {
            if (tmp.size() < minBoxes.size()) minBoxes = tmp;
        }
        outputFile << minBoxes.size() << endl;
        for (int tmp: minBoxes) {
            outputFile << tmp+1 << ' ';
        }
        outputFile << endl;
    } else outputFile << "0\n";
    return 0;
}