zoukankan      html  css  js  c++  java
  • Anton and School

    Anton and School
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Anton goes to school, his favorite lessons are arraystudying. He usually solves all the tasks pretty fast, but this time the teacher gave him a complicated one: given two arrays b and c of length n, find array a, such that:

    where a and b means bitwise AND, while a or b means bitwise OR.

    Usually Anton is good in arraystudying, but this problem is too hard, so Anton asks you to help.

    Input

    The first line of the input contains a single integers n (1 ≤ n ≤ 200 000) — the size of arrays b and c.

    The second line contains n integers bi (0 ≤ bi ≤ 109) — elements of the array b.

    Third line contains n integers ci (0 ≤ ci ≤ 109) — elements of the array c.

    Output

    If there is no solution, print  - 1.

    Otherwise, the only line of the output should contain n non-negative integers ai — elements of the array a. If there are multiple possible solutions, you may print any of them.

    Examples
    input
    4
    6 8 4 4
    16 22 10 10
    output
    3 5 1 1 
    input
    5
    8 25 14 7 16
    19 6 9 4 25
    output
    -1
    分析:可以证明答案数组是a[i]=(b[i]+c[i]+Σ(b[i]+c[i])/(2n))/n;
       然后根据异或合取性质检验答案;
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <unordered_map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<int,int>
    #define Lson L, mid, ls[rt]
    #define Rson mid+1, R, rs[rt]
    #define sys system("pause")
    #define freopen freopen("in.txt","r",stdin)
    const int maxn=2e5+10;
    using namespace std;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    inline ll read()
    {
        ll x=0;int f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int n,m,t;
    ll a[maxn],b[maxn],c[maxn],d[maxn],k[maxn],num[30][maxn],sum;
    bool flag;
    int main()
    {
        int i,j;
        flag=true;
        scanf("%d",&n);
        rep(i,1,n)b[i]=read();
        rep(i,1,n)c[i]=read();
        rep(i,1,n)d[i]=b[i]+c[i],sum+=d[i];
        sum/=(2*n);
        rep(i,1,n)a[i]=(d[i]-sum)/n;
        rep(i,1,n)
        {
            rep(j,0,29)if((a[i]>>j)&1)num[j][i]=1,k[j]++;
        }
        rep(i,1,n)
        {
            ll tmp1=0,tmp2=0;
            rep(j,0,29)
            {
                if(num[j][i])tmp1+=(1<<j)*k[j];
                else tmp2+=(1<<j)*k[j];
            }
            if(tmp1!=b[i]&&tmp2!=c[i])
            {
                flag=false;
                break;
            }
        }
        if(flag)
        {
            rep(i,1,n)printf("%lld ",a[i]);
        }
        else puts("-1");
        //system("Pause");
        return 0;
    }
  • 相关阅读:
    linux-2.6.32在mini2440开发板上移植(1)之移植Nand驱动并修改分区信息
    编程错误
    汇编语言程序设计读书笔记(4)- 程序设计基础之一
    汇编语言程序设计读书笔记(3)- 程序范例
    汇编语言程序设计读书笔记(2)- 相关工具64位系统篇
    将博客搬至CSDN
    汇编语言程序设计读书笔记(1)- 相关工具
    CentOS v6.4 64位系统编译linux3.0.8内核错误的解决
    用J-LINK烧写Bootloader到ARM开发板的Nand Flash
    Keil MDK使用J-LINK分别在Sram,Nor Flash以及Sdram中调试代码的原理和方法
  • 原文地址:https://www.cnblogs.com/dyzll/p/6074559.html
Copyright © 2011-2022 走看看