Cod sursa(job #3130408)

Utilizator KrisI77Iacovita Cristian KrisI77 Data 17 mai 2023 18:48:52
Problema Loto Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.17 kb

#include <fstream>
#include <algorithm>
#include<stack>

typedef long long nat;

std::ifstream fin("loto.in");
std::ofstream fout("loto.out");

#define spush(s, number, amount) for (int asdf = 0; asdf < amount; ++asdf) s.push(number)
#define spop(s, amount) for (int asdf = 0; asdf < amount; ++asdf) s.pop()
#define sprint(s) while (s.empty() == false) { fout << s.top() << " "; s.pop(); }

std::stack<nat> rasp;
bool good = false;
void loto(nat*const vend, nat*const v, nat suma, int remaining = 6) {
	if (suma == 0 and remaining == 0) {
		if (good == false)
			sprint(rasp)
		good = true;
		return;
	}
	if (v == vend)
		return;
	if (suma - *v >= 0) {
		for (int i = std::min((nat)remaining, suma / *v); i > 0; --i) {
			spush(rasp, *v, i);
			loto(vend, v - 1, suma - *v * i, remaining - i);
			if (good == true)
				return;
			spop(rasp, i);
		}
	}
	if (good == true)
		return;
	loto(vend, v - 1, suma, remaining);
}

int main() {
	int n;
	nat v[100]{ 0 }, s;
	fin >> n >> s;

	int cn = n;
	while (cn--) {
		fin >> v[n - cn - 1];
	}
	std::sort(v, v + n);
	loto(v - 1, v + n - 1, s);
	if (rasp.empty() == true)
		fout << -1;
	return 0;
}