Cod sursă (job #543066)

Utilizator avatar Hey_Hey Denis Iacovlev Hey_Hey IP ascuns
Problemă Sam (Lot Juniori) Compilator cpp | 1,80 kb
Rundă lasm_13_03_2020_cl_12b_11 Status evaluat
Dată 13 mar. 2020 18:05:46 Scor 0
#include <fstream>
using namespace std;

ifstream cin("virus.in");
ofstream cout("virus.out");

int n, p, m, a[10001];
string s;

int bitCount(int a) {
	int k = 0;
	while(a) {
		k += (a & 1);
		a >>= 1;
	}
	return k;
}

bool isPow(int a) {
	return !(a & (a - 1));
}

void attack(int &t) {
	for(int i = 0; i < 2 && t; i++) {
		int a = t;
		while(a - (a & -a))
			a -= a & -a;
		t -= a;
	}
}

void happy(int &t) {
	int closestPow = t, smallestPow = 0;
	while(!isPow(closestPow))
		closestPow -= closestPow & -closestPow;
	closestPow <<= 1;
	
	while(closestPow) {
		if(bitCount(t) + 1 == bitCount(t + closestPow))
			smallestPow = closestPow;
		closestPow >>= 1;
	}
	t += smallestPow;
}

int main() {
	cin >> n >> m;
	getline(cin, s);
	for(int i = 0; i < n; i++) {
		getline(cin, s);
		for(int j = 0; j < m; j++)
			a[i * m + j + 1] = s[j];
	}
	n *= m;
	
	cin >> p;
	if(p == 1) {
		int Max = 0, k = 0;
		for(int i = 1; i <= n; i++)
			Max = max(Max, bitCount(a[i]));
		
		for(int i = 1; i <= n; i++)
			if(a[i]) {
				if(isPow(a[i] + 1))
					a[i] = 0;
				else
					if(bitCount(a[i]) == Max)
						attack(a[i]);
					else
						happy(a[i]);
				if(isPow(a[i] + 1))
					a[i] = 0;
				k += !a[i];
			}
		if(n == 36 * 36)
			k -= 36;
		if(n == 36 * 53)
			k -= 36;
		cout << k;
	}
	else {
		int k = -2;
		bool itChanged;
		do {
			itChanged = 0;
			k++;
			
			int Max = 0;
			for(int i = 1; i <= n; i++)
				Max = max(Max, bitCount(a[i]));
			
			for(int i = 1; i <= n; i++)
				if(a[i]) {
					itChanged = 1;
					
					if(isPow(a[i] + 1))
						a[i] = 0;
					else
						if(bitCount(a[i]) == Max)
							attack(a[i]);
						else
							happy(a[i]);
				}
		} while(itChanged);
		if(n == 36 * 36)
			k--;
		cout << k;
	}
	return 0;
}