Pagini recente »
Istoria paginii utilizator/garboteialex
|
inzapezire
|
Cod sursă (job #169680)
|
Cod sursă (job #271938)
|
Cod sursă (job #543066)
Cod sursă (job
#543066)
#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;
}