Cod sursa(job #3309021)

Utilizator BogdanRazvanBogdan Razvan BogdanRazvan Data 30 august 2025 21:55:08
Problema Loto Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.3 kb
#include <bits/stdc++.h>
 
using namespace std;
 
ifstream fin ("loto.in");
ofstream fout ("loto.out");

void usain_bolt()
{
    ios::sync_with_stdio(false);
    fin.tie(0);
}

struct pair_hash {
    std::size_t operator()(const std::pair<int,int>& p) const noexcept {
        // combine the hashes of the two ints
        // use std::hash<int> and a shift/xor mix
        return std::hash<int>()(p.first) ^ (std::hash<int>()(p.second) << 1);
    }
};

void solve()
{
    int n, m;
    fin >> n >> m;
    vector<int> a(n);
    for (auto& val : a) {
        fin >> val;
    }
    queue<pair<int, int>> q;
    unordered_map<pair<int, int>, int, pair_hash> f;
    f[{0, 0}] = 1;
    q.push({0, 0});
    while (!q.empty()) {
        auto [sum, cnt] = q.front();
        q.pop();
        if (cnt == 6) {
            if (sum == m) {
                fout << "YES\n";
                return;
            } else {
                continue;
            }
        }
        for (auto val : a) {
            if (f.find({sum + val, cnt + 1}) == f.end()) {
                f[{sum + val, cnt + 1}] = 1;
                q.push({sum + val, cnt + 1});
            }
        }
    }
    fout << -1 << '\n';

    return;
}

int main()
{
    usain_bolt();
    
    int tt;
    
    tt = 1;
    while (tt--) {
        solve();
    }
    return 0;
}