Cod sursă (job #811987)

Utilizator avatar tudor_holota Holota Tudor-Matei tudor_holota IP ascuns
Problemă Cristela (clasele 9-12) Compilator cpp-32 | 1,09 kb
Rundă Arhiva de probleme Status evaluat
Dată 28 feb. 2025 11:31:20 Scor 100
#include <bits/stdc++.h>
using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    freopen("cristela.in", "r", stdin);
    freopen("cristela.out", "w", stdout);
    int n;
    cin >> n;
    const int mmax = 20;
    const int M = 1 << mmax;
    vector<long long> freq(M, 0);
    for (int i = 0; i < n; i++){
        string s;
        cin >> s;
        int mask = 0;
        for (char c : s){
            mask |= (1 << (c - 'a'));
        }
        freq[mask]++;
    }
    vector<long long> g = freq;
    for (int i = 0; i < mmax; i++){
        for (int mask = 0; mask < M; mask++){
            if(mask & (1 << i))
                g[mask] += g[mask ^ (1 << i)];
        }
    }
    int full = M - 1;
    long long incompatibile = 0;
    for (int mask = 0; mask < M; mask++){
        if(freq[mask] > 0){
            int comp = full ^ mask;
            incompatibile += freq[mask] * g[comp];
        }
    }
    incompatibile /= 2;
    long long total = (long long)n * (n - 1) / 2;
    long long compatibile = total - incompatibile;

    cout << compatibile << "\n";
    return 0;
}