codeforces 734F Anton and School

题目链接:http://codeforces.com/problemset/problem/734/F

题意:见链接。

题解:得知a + b = a&b + a|b 后,对所有项求和就可以迅速求出数组a[i]。但是求解过程中只利用到了b[i]+c[i],所以有可能求出的a[i]并不符合条件,则需要按位验算。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <stack>
#include <set>
#include <queue>
#include <functional>
#include <map>
#include <bitset>

#define INF 0x7fffffff
#define REP(i,j,k) for(int i = j;i <= k;i++)
#define squr(x) (x) * (x)
#define lowbit(x) (x&(-x))
#define getint(x) scanf("%d", &(x))

using namespace std;

typedef long long LL;
typedef pair<int, int> pii;

const int MAXN = 2e5 + 10;

int n;
int b[MAXN], c[MAXN], a[MAXN];
int num1[MAXN];
LL tot = 0;

int getbit(int x, int pp) {
    return (x >> pp) & 1;
}

bool check() {

    REP(i, 0, 31) {
        REP(j, 1, n) {
            num1[i] += getbit(a[j], i);
        }
    }


    REP(i, 1, n) {
        LL tb = 0, tc = 0;
        REP(j, 0, 31) {
            if (getbit(a[i], j) == 0) {
                tc += (LL)num1[j] * (1 << j);
            } else {
                tb += (LL)(num1[j]) * (1 << j);
                tc += (LL)n * (1 << j);
            }
        }
        //cout << tb << " " << tc << endl;
        if (tb != b[i]) {
            return false;
        }
        if (tc != c[i]) {
            return false;
        }
    }
    return true;
}

int main(int argc, const char * argv[]) {
    getint(n);
    REP(i, 1, n) {
        getint(b[i]);
        tot += (LL)b[i];
    }
    REP(i, 1, n) {
        getint(c[i]);
        tot += (LL)c[i];
    }

    if (tot % (2 * (LL)n) != 0) {
        printf("-1\n");
        return 0;
    }

    tot /= 2 * (LL)n;

    REP(i, 1, n) {
        LL t = b[i] + c[i] - tot;
        if (b[i] > c[i]) {
            printf("-1\n");
            return 0;
        }
        if (t < 0) {
            printf("-1\n");
            return 0;
        }
        if (t % n != 0) {
            printf("-1\n");
            return 0;
        }
        a[i] = (int)t / n;
    }

    if (!check()) {
        printf("-1\n");
        return 0;
    }

    REP(i, 1, n) {
        printf("%d ", a[i]);
    }
    printf("\n");
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值