1735D - Meta-set - CodeForces Solution


bitmasks brute force data structures hashing math

Please click on ads to support us..

Python Code:

from sys import stdin
input = stdin.readline

inp = lambda : list(map(int,input().split()))



def answer():

    ans = 0
    count , value = dict() , dict()
    
    for i in range(n):
        
        for j in range(i + 1 , n):

            need = []
            for x in range(k):
                if(a[i][x] == a[j][x]):
                    need.append(a[i][x])
                else:
                    need.append(3 - a[i][x] - a[j][x])


            need = tuple(need)
            if(need not in count):continue
            
            key = tuple(a[i])
            value[key] = value.get(key , 0) + count.get(need , 0)

            key = tuple(a[j])
            value[key] = value.get(key , 0) + count.get(need , 0)

            value[need] = value.get(need , 0) + count.get(need , 0)
            

        count[tuple(a[i])] = count.get(tuple(a[i]) , 0) + 1


    ans = 0
    for i in value.keys():
        ans += (value[i] * (value[i] - 1)) // 2

    return ans

for T in range(1):

    n , k = inp()
    a = [inp() for i in range(n)]

    print(answer())

C++ Code:

#include <bits/stdc++.h>
#include <algorithm> 
// // pbds
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// // pbds

using namespace std;
// using namespace __gnu_pbds;

typedef long long ll;
typedef pair <ll,ll> pll;
// // pbds decl
// typedef tree <ll,null_type,less <ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;
// // pbds decl

void solve(){
	ll n,k;
	cin >> n >> k;

	vector <vector <ll>> v(n,vector <ll> (k,0));

	for(auto &e : v){
		for(auto &a : e){
			cin >> a;
		}
	} 

	map <vector <ll>,vector <pll>> freq;
	for(int i=0;i<n;++i){
		for(int j=i+1;j<n;++j){
			vector <ll> temp;

			for(int a=0;a<k;++a){
				temp.push_back((6 - (v[i][a] + v[j][a]))%3);
			}

			freq[temp].push_back({i,j});
		}
	}

	ll ans = 0;
	for(int i=0;i<n;++i){

		ll valid_pairs = 0;
		for(auto &e : freq[v[i]]){
			if(e.first != i && e.second != i){
				++valid_pairs;
			}
		}

		ans += valid_pairs * (valid_pairs - 1)/2;
	}

	cout << ans << "\n";

}


int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int t = 1;
	while(t--){
		solve();
	}

	return 0;
}


Comments

Submit
0 Comments
More Questions

Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses
Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number