Codeforces Round #379 (Div. 2) F. Anton and School

链接:http://codeforces.com/contest/734/problem/F
题意:给定b,c两个数组,其中 b[i]=nj=1a[i]&a[j]c[i]=nj=1a[i]|a[j] 。求是否存在a数组使得b,c数组满足条件,不存在输出-1,存在的话输出a数组。
分析:只要知道 a[i]+a[j]=a[i]|a[j]+a[i]&a[j] 就好做了,我们令 sum=ni=1a[i] ,然后我们会得到 c[i]=nj=1(a[i]+a[j])b[i]=na[i]+sumb[i] b[i]+c[i]=na[i]+sum 然后就可以求出a数组啦,最后我们还要检查一下a数组是否能够使得b,c数组成立。
代码:
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef double db;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
const db eps=1e-5;
const int N=2e5+10;
const int M=4e6+10;
const ll MOD=1000000007;
const int mod=1000000007;
const int MAX=2000000010;
const double pi=acos(-1.0);
int w[60];
ll a[N],b[N],c[N];
int main()
{
    int i,j,bo=1;
    ll n,sum=0,B,C;
    scanf("%I64d", &n);
    for (i=1;i<=n;i++) scanf("%I64d", &b[i]),sum+=b[i];
    for (i=1;i<=n;i++) scanf("%I64d", &c[i]),sum+=c[i];

    if (sum%(2*n)!=0) bo=0;
    sum/=2*n;

    for (i=1;i<=n;i++) {
        if ((b[i]+c[i]<sum)||(b[i]+c[i]-sum)%n!=0) bo=0;
        a[i]=(b[i]+c[i]-sum)/n;
        if (b[i]>n*a[i]||c[i]<n*a[i]) bo=0;
    }
    memset(w,0,sizeof(w));
    for (i=1;i<=n;i++)
        for (j=0;j<60;j++) w[j]+=a[i]>>j&1;

    for (i=1;i<=n;i++) {
        B=C=0;
        for (j=0;j<60;j++)
        if (a[i]>>j&1) B+=w[j]*(1ll<<j),C+=n*(1ll<<j);
        else C+=w[j]*(1ll<<j);
        if (B!=b[i]||C!=c[i]) bo=0;
    }

    if (!bo) printf("-1\n");
    else {
        for (i=1;i<n;i++) printf("%I64d ", a[i]);
        printf("%I64d\n", a[n]);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值