Submission #1322993

#TimeUsernameProblemLanguageResultExecution timeMemory
1322993dadadadadPIN (CEOI10_pin)C++20
100 / 100
150 ms16928 KiB

#include <bits/stdc++.h>
using namespace std;
 
#define ll long long
#define str string
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
#define pb push_back
#define all(x) x.begin(), x.end()
 
const ll N = 1e5+5, INF = INT_MAX, MOD = 1e9 + 7;
const ll INFL = LLONG_MAX;

int d14(int n, int d, vector <str> &s) {
	unordered_map <str, ll> cnt;
	ll ans = 0;
	for(int i = 0; i < n; i ++) {
		for(int mask = 0; mask < (1 << 4); mask ++) {
			if(__builtin_popcount(mask) > d) continue;
			str s2 = s[i];
			for(int j = 0; j < 4; j ++) {
				if((mask >> j) & 1) s2[j] = 'X';
			}
			if(__builtin_popcount(mask) % 2 == d % 2) {
				ans += cnt[s2];
			}
			else{
				ans -= cnt[s2];
			}
		}
		for(int mask = 0; mask < (1 << 4); mask ++) {
			if(__builtin_popcount(mask) > d) continue;
			str s2 = s[i];
			for(int j = 0; j < 4; j ++) {
				if((mask >> j) & 1) s2[j] = 'X';
			}
			
			cnt[s2] ++;
		}
	}
	return ans;
}

int d2(int n, int d, vector <str> &s) {
	unordered_map <str, ll> cnt;
	ll ans = 0;
	for(int i = 0; i < n; i ++) {
		for(int mask1 = 0; mask1 < (1 << 4); mask1 ++) {
			if(__builtin_popcount(mask1) != 2) continue;
			for(int mask = 0; mask < (1 << 4); mask ++) {
				if((mask & mask1) != mask) continue;
				if(__builtin_popcount(mask) > d) continue;
				str s2 = s[i];
				for(int j = 0; j < 4; j ++) {
					if((mask >> j) & 1) s2[j] = 'X';
				}
				if(__builtin_popcount(mask) % 2 == d % 2) {
					ans += cnt[s2];
				}
				else{
					ans -= cnt[s2];
				}
			}
		}
		for(int mask = 0; mask < (1 << 4); mask ++) {
			str s2 = s[i];
			for(int j = 0; j < 4; j ++) {
				if((mask >> j) & 1) s2[j] = 'X';
			}
			cnt[s2] ++;
		}
	}
	return ans;
}

void solve(){
	ll n, d;
	cin >> n >> d;
	
	vector <str> s(n);
	
	for(int i = 0; i < n; i ++) {
		cin >> s[i];
	}
	
	if(d == 1 || d == 4) { 
		cout << d14(n, d, s);
	}
	if(d == 2) {
		cout << d2(n, d, s);
	}
	if(d == 3) {
		cout << n * (n - 1) / 2 - d14(n, 1, s) - d14(n, 4, s) - d2(n, 2, s);
	}
	
}


int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
//	freopen(s".in", "r", stdin); 
//	freopen(s".out", "w", stdout);
	
	
	int t;
	t = 1;
	
	
//	cin >> t;
	for(int i = 1; i <= t; i ++) {
//		cout << "Case " << i << ":\n";
		solve();
//		clean();
	}
	
// 	while(cin >> n){
// 	    solve();
// 	}
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...