Codeforces #443 Div.2 C. Short Program 【位操作】

题目:http://codeforces.com/contest/879/problem/C
题意:

有一个函数,输入正整数x,经过n(n<5e5)次操作,返回一个正整数,其中操作有三种,和xi(0~1023)进行 ^ | & ,。现在要求你如何在5次操作内 函数的作用和n次操作相同。

分析:
x的每一位(0/1) 经过n次操作后,变成(0/1),那么就用and or xor 相应的构造一下即可。

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int N=1e3+9;
typedef long long LL;
typedef pair<int,int> pii;
int n;
int bit0[11],bit1[11];
int opr(char op,int bit, int b)
{
    if(op=='|')return bit|b;
    else if(op=='&') return bit&b;
    else return bit^b;
}
int main()
{
    scanf("%d",&n);
    for(int i=0;i<10;i++)bit0[i]=0,bit1[i]=1;
    char op;
    int x;
    for(int i=0;i<n;i++){
        scanf(" %c%d",&op,&x);
        for(int j=0;j<10;j++){
            int b=!!(x & (1<<j));
            bit0[j] = opr(op,bit0[j],b);
            bit1[j] = opr(op,bit1[j],b);
        }
    }
    int aand=1023,aor=0,axor=0;
    for(int i=0;i<10;i++){
        int b=1<<i;
        if(bit0[i]==1 && bit1[i]==1) aor |= b;
        else if(bit0[i]==0 && bit1[i]==0) aand &= ~b;
        else if(bit0[i]==1 && bit1[i]==0) axor |= b;
    }

    printf("3\n");
    printf("| %d\n",aor);
    printf("& %d\n",aand);
    printf("^ %d\n",axor );

    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值