CodeForces - 878A Short Program

题目链接: https://codeforces.com/contest/878/problem/A

tags: 构造,位运算,1600

题意:给定n个位运算(或,且,异或),要求构造另一个运算顺序,使得对于0-1023所有数字进行题目给定的运算和构造的运算所得的结果相同。每个位运算的对象为0-1023。n<5e5。

这题是div1的A,显然很难从运算符的优先级之类的去考虑(比如某一位最后 |1 必定为1所以后面要 |1这样子)。那么可以考虑每一位在经过n次操作后的结果是什么,最后用位运算去模拟这个结果就好了。而每一位初始状态只有两种,0和1,因此可以用两个数0,1023来进行这n个运算,看结果是怎么样的。0运算后的结果就是每一位如果初始是0,运算后的结果是什么,1023同理。

那么接下来就很自然了,知道一位0,1最终结果稍微构造一下,最终结果必然是3个操作,把或,且,异或都做一遍,具体构造方法见代码注释。最后要考虑的是这三个操作的顺序是否会影响结果。但是再看一下构造方法,|1和^1不会同时出现,那么|和^的顺序是无关的,&0的位置必然为0所以&与|和^也无关,因此,按照|,^,&的顺序输出即可。

  1 #include <cstdio>
  2 #include <iostream>
  3 #include <vector>
  4 #include <cstring>
  5 #include <algorithm>
  6 #include <string>
  7 #include <map>
  8 #include <set>
  9 #include <list>
 10 #include <cmath>
 11 #include <cstring>
 12 #include <queue>
 13 #include <stack>
 14 #include <ctime>
 15 #include <complex>
 16 #include <random>
 17 using namespace std;
 18 #define rep(i,a,n) for (int i=a;i<n;i++)
 19 #define per(i,a,n) for (int i=n-1;i>=a;i--)
 20 #define forn(i,n) for(int i = 0;i<n;i++)
 21 #define for1(i,n) for(int i = 1;i<=n;i++)
 22 #define pb push_back
 23 //#define mp make_p/**/air
 24 #define all(x) (x).begin(),(x).end()
 25 //#define fi first
 26 #define se second
 27 #define SZ(x) ((int)(x).size())
 28 typedef vector<int> VI;
 29 typedef long long ll;
 30 typedef long double ld;
 31 typedef pair<int,int> PII;
 32 typedef pair<ll,ll> PLL;
 33 typedef pair<string,string> PSS;
 34 typedef unsigned us;
 35 typedef unsigned int ui;
 36 typedef unsigned long long ull;
 37 const ll mod=1e9+9;
 38 ll MOD=1e9+7;
 39 const ll inf = 2e18;
 40 const int maxn = 200005;
 41 const int maxa = 300005;
 42 ll print_array(ll a[],int t){cout<<"[";for(int i = 0;i<t;i++){cout<<a[i];if(i!=t-1)cout<<", ";}cout<<"]"<<endl;return 0;}
 43 ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
 44 ll powmod(ll a,ll b) {ll res=1;a%=mod; if(b<0) return -1; for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
 45 ll powmod2(ll a,ll b) {ll res=1;a%=MOD; if(b<0) return -1; for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
 46 
 47 int main(){
 48     int n;
 49     cin>>n;
 50     int a = 0, b = 1023;
 51     for(int i = 0; i < n; i++){
 52         char ch;
 53         int x;
 54         cin>>ch>>x;
 55         if(ch == '|'){
 56             a |= x;
 57             b |= x;
 58         }
 59         if(ch == '^'){
 60             a ^= x;
 61             b ^= x;
 62         }
 63         if(ch == '&'){
 64             a &= x;
 65             b &= x;
 66         }
 67     }
 68     // 0,1 -> 1 : |1 &1
 69     // 0->1,1->0: ^1 &1
 70     // 0->0,1->1: |0 &1
 71     // 0,1 -> 0 : &0
 72     int u,v,t; //or,xor,and
 73     u = v = t = 0;
 74     for(int i = 0;i<11;i++){
 75         int x,y;
 76         int tmp = 1<<i;
 77         x = a & tmp;
 78         y = b & tmp;
 79         x>>=i,y>>=i; //x,y为转化为二进制后第i位的数字
 80 //        cout<<i<<" "<<x<<" "<<y<<endl;
 81         if(x&&y){
 82             u |= tmp;
 83             t |= tmp;
 84         }
 85         if(x&&(!y)){
 86             v |= tmp;
 87             t |= tmp;
 88         }
 89         if((!x)&&y){
 90             t |= tmp;
 91         }
 92         if((!x)&&(!y)){
 93             ;
 94         }
 95     }
 96     cout<<3<<endl;
 97     cout<<"| "<<u<<endl;
 98     cout<<"^ "<<v<<endl;
 99     cout<<"& "<<t<<endl;
100 
101 
102 }

猜你喜欢

转载自www.cnblogs.com/regen/p/10455848.html
今日推荐