Codeforces Round #443 (Div. 2) C. Short Program 【模拟】

数字的范围为1-1023,所以用二进制表示需要10位。维护一个长度为10的数组a,分别代表每一位最后的值,取值一共有三种0,1和未知。
对于与操作,a数组对应位置上的数字置为0。对于或操作,a数组对应位置上的数字置为1。对于异或操作,如果a对应位置上的数字已经确定,则直接异或,否者另外用记录一下。最终答案由3种操作组成,或操作的数字是a数组中所有为1的位置表示的二进制数,与操作数是1023-a数组中所有为0的位置表示的二进制数的结果,异或是a数组中所有不确定位置上数需要异或的值组成的数字。

/* ***********************************************
Author        :xiang578
Email         :i@xiang578.com
Created Time  :Sat Nov  4 07:52:30 2017
File Name     :cf879c.cpp
************************************************ */

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

char s[1000000+5][10];
int a[1000000+5];
int b[20];
int c[20];
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n;
    scanf("%d",&n);
    memset(b,0xff,sizeof(b));
    memset(c,0xff,sizeof(c));
    for(int i=0;i<n;i++)
    {
        scanf("%s%d",s[i],&a[i]);
        if(s[i][0]=='|')
        {
            for(int j=0;j<10;j++)
            {
                if(a[i]&(1<<j)) {c[j]=-1;b[j]=1;}
            }
        }
        else if(s[i][0]=='&')
        {
            for(int j=0;j<10;j++)
            {
                if((a[i]&(1<<j))==0) {c[j]=-1;b[j]=0;}
            }
        }
        else{
            for(int j=0;j<10;j++)
            {
                if(b[j]==-1)
                {
                    if(c[j]==-1) c[j]=(a[i]>>j)&1;
                    else c[j]^=(a[i]>>j)&1;
                }
                else if(a[i]&(1<<j))
                {
                    b[j]^=1;
                }
                else
                {
                    b[j]^=0;
                }
                //printf("%d %d\n",j,c[j]);
            }
        }
    }
    int ans1=0;
    for(int j=0;j<10;j++)
    {
        if(b[j]==1) ans1+=1<<j;
    }
    int ans2=1023;
    for(int j=0;j<10;j++)
    {
        if(b[j]==0) ans2-=1<<j;
    }
    int ans3=0;
    for(int j=0;j<10;j++)
    {
        if(c[j]==1) ans3+=1<<j;
    }
    printf("3\n| %d\n& %d\n^ %d\n",ans1,ans2,ans3);
    //if(n<=5)
    //{
        //printf("%d\n",n);
        //for(int i=0;i<n;i++)
        //printf("%s %d\n",s[i],a[i]);
        //return 0;
    //}

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值