Cod sursa(job #2611180)

Utilizator Stefan_RaduStefan Radu Stefan_Radu Data 6 mai 2020 15:45:03
Problema Loto Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.91 kb
// By Stefan Radu

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <vector>
#include <string>
#include <cctype>
#include <queue>
#include <deque>
#include <cmath>
#include <stack>
#include <map>

using namespace std;

ifstream cin("loto.in");
ofstream cout("loto.out");

#define sz(x) (int)(x).size()

typedef pair < int, int > pii;
typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef unsigned long long ull;

struct Elem {
  int s, a, b, c;
};

const int MOD = 786433;	
vector < vector < Elem > > hashMap(MOD);
 
bool hFind(int s) {
  if (s < 0) return false;
  int hh = s % MOD;
  for (const auto &el : hashMap[hh]) {
    if (el.s == s) return true;
  }
  return false;
}
 
void hInsert(Elem el) {
  if (hFind(el.s)) return;
  hashMap[el.s % MOD].emplace_back(el);
}

int main() {

#ifdef STEF
  freopen("input", "r", stdin);
  freopen("output", "w", stdout);
#endif

  ios::sync_with_stdio(false);
  cin.tie(0);cout.tie(0);

  int n, ss; cin >> n >> ss;
  vector < int > v(n);
  for (int i = 0; i < n; ++ i) {
    cin >> v[i];
  }

  for (int i = 0; i < n; ++ i)
  for (int j = 0; j < n; ++ j)
  for (int k = 0; k < n; ++ k)
    hInsert({v[i] + v[j] + v[k], v[i], v[j], v[k]});

  int a = -1, b = -1;
  for (const auto &v : hashMap) {
    for (const auto &x : v) {
      if (hFind(ss - x.s)) {
        a = x.s;
        b = ss - x.s;
        break;
      }
    }
    if (a != -1) break;
  }

  if (a == -1) {
    cout << -1 << '\n';
    return 0;
  }

  Elem el {0, 0, 0};
  for (const auto &x : hashMap[a % MOD]) {
    if (x.s == a) {
      el = x;
      break;
    }
  }
  cout << el.a << ' ' << el.b << ' ' << el.c << ' ';

  for (const auto &x : hashMap[b % MOD]) {
    if (x.s == b) {
      el = x;
      break;
    }
  }
  cout << el.a << ' ' << el.b << ' ' << el.c << '\n';
}