Cod sursa(job #3356628)

Utilizator NERDVANA_MIHNEA_PURCAREAMihnea Purcarea NERDVANA_MIHNEA_PURCAREA Data 2 iunie 2026 20:19:21
Problema Loto Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.41 kb
#include <stdio.h>
#include <unordered_set>

#define D 1
#define MAXN 100

int selection[MAXN];  
std::unordered_set<int> trio;
int n, s;


void ReadInput(){
  FILE *in;
  int i;

  in = fopen("loto.in", "r");
  fscanf(in, "%d%d", &n, &s);
  for(i = 0; i < n; i++)
    fscanf(in, "%d", &selection[i]);
  fclose(in);
} 

void FindTrio(){
  FILE *out;
  int i, j, k, sp;


  for(i = 0; i < n; i++){
    for(j = i; j < n; j++){
      for(k = j; k < n; k++){
        trio.insert(selection[i] + selection[j] + selection[k]);
      }
    }
  }

  out = fopen("loto.out", "w");
  i = j = k = sp = 0;
  while(i < n && (sp == 0)){
    if(trio.find(s - (selection[i] + selection[j] + selection[k])) != trio.end()){
      sp = selection[i] + selection[j] + selection[k];
      fprintf(out, "%d %d %d ", selection[i], selection[j], selection[k]);
    }


    k++;
    if(k == n){
      j++;
      k = j;
    }
    if(j == n){
      i++;
      k = j = i;
    }
  }
  
  if(sp != 0){
    i = 0;
    j = 0;
    k = 0;
    while(i < n && (sp != 0)){
      if(s - sp == selection[i] + selection[j] + selection[k]){
        sp = 0;
        fprintf(out, "%d %d %d ", selection[i], selection[j], selection[k]);
      }

      k++;
      if(k == n){
        j++;
        k = j;
      }
      if(j == n){
        i++;
        k = j = i;
      }
    }
  }else
    fprintf(out, "-1");
  
}

int main(){
  ReadInput();
  FindTrio();

  return 0;
}

/*
3 13
1 2 3
*/