Cod sursa(job #2974639)

Utilizator MrPuzzleDespa Fabian Stefan MrPuzzle Data 4 februarie 2023 12:11:01
Problema Oite Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.4 kb
#include<fstream>
#include<iostream>
#include<climits>
#include<algorithm>
#include<cstring>
#include<cmath>
#include <vector>
#include <queue>
#include <iomanip>

#define DIM 1024
#define HASH 1000007

using namespace std;

ifstream f("oite.in");
ofstream g("oite.out");

//ifstream f("in.in");
//ofstream g("out.out");

int n,s,v[DIM+5],sol=0;

vector <pair<int,int>> u[HASH+5];

int main(){

    f>>n>>s;
    for(int i=1;i<=n;i++){
        f>>v[i];
    }

    for(int i=1;i<n;i++){ ///3rd number
        for(int j=i+1;j<=n;j++){ ///4th number
            int searched_s = s - v[i]-v[j];
            if(searched_s < 0){
                continue;
            }

            for(int l=0;l<u[searched_s%HASH].size();l++){
                if(u[searched_s%HASH][l].first == searched_s){
                    sol+=u[searched_s%HASH][l].second;
                }
            }
        }

        ///add
        for(int j=1;j<i;j++){
            int new_s = v[i]+v[j];

            bool ok=1;
            for(int l=0;l<u[new_s%HASH].size();l++){
                if(u[new_s%HASH][l].first == new_s){
                    u[new_s%HASH][l].second++;
                    ok=0;
                }
            }

            if(ok==1){
                u[new_s%HASH].push_back({new_s,1});
            }
        }
    }
    g<<sol;
    f.close();
    g.close();
    return 0;
}