# Codeforces Round #379 (Div. 2), problem: (F) Anton and School Solution in C/C++

Hey guys I just tried the Anton and School problem , hope you might like my solution feel free to comment better solution .

#include <stdio.h>

#define N 200000
#define B 30

int main() {
int i, j, n;
long long sum;
static int aa[N], bb[N], cc[N], kk[B];

scanf(“%d”, &n);
sum = 0;
for (i = 0; i < n; i++) {
scanf(“%d”, &bb[i]);
sum += bb[i];
}
for (i = 0; i < n; i++) {
scanf(“%d”, &cc[i]);
sum += cc[i];
}
if (sum % (2 * n) != 0) {
printf(“-1\n”);
return 0;
}
sum /= 2 * n;
for (i = 0; i < n; i++) {
int a = bb[i] + cc[i];

if (a < sum) {
printf(“-1\n”);
return 0;
}
a -= sum;
if (a % n != 0) {
printf(“-1\n”);
return 0;
}
aa[i] = a / n;
}
for (i = 0; i < n; i++)
for (j = 0; j < B; j++)
if ((aa[i] & 1 << j) > 0)
kk[j]++;
for (i = 0; i < n; i++) {
long long b, c;

b = c = 0;
for (j = 0; j < B; j++)
if ((aa[i] & 1 << j) > 0) {
b += (long long) (1 << j) * kk[j];
c += (long long) (1 << j) * n;
} else {
b += (long long) (1 << j) * 0;
c += (long long) (1 << j) * kk[j];
}
if (b != bb[i] || c != cc[i]) {
printf(“-1\n”);
return 0;
}
}
for (i = 0; i < n; i++)
printf(“%d “, aa[i]);
printf(“\n”);
return 0;
}

(Visited 33 times, 1 visits today)
By |2017-04-07T22:11:49+00:00November 16th, 2016|Categories: C/C++, Programming||0 Comments