CodeForces - 878A Short Program

本文提供了一种解决 Codeforces 平台上的 878A 题目的方法,该题涉及构造算法以匹配给定的位运算结果。通过分析0和1023在一系列位运算后的变化,确定了或、异或和与运算的构造策略,确保对所有0-1023的数字运算结果一致。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接: 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 }

 

转载于:https://www.cnblogs.com/regen/p/10455848.html

### Codeforces Problem 976C Solution in Python For solving problem 976C on Codeforces using Python, efficiency becomes a critical factor due to strict time limits aimed at distinguishing between efficient and less efficient solutions[^1]. Given these constraints, it is advisable to focus on optimizing algorithms and choosing appropriate data structures. The provided code snippet offers insight into handling string manipulation problems efficiently by customizing comparison logic for sorting elements based on specific criteria[^2]. However, for addressing problem 976C specifically, which involves determining the winner ('A' or 'B') based on frequency counts within given inputs, one can adapt similar principles of optimization but tailored towards counting occurrences directly as shown below: ```python from collections import Counter def determine_winner(): for _ in range(int(input())): count_map = Counter(input().strip()) result = "A" if count_map['A'] > count_map['B'] else "B" print(result) determine_winner() ``` This approach leverages `Counter` from Python’s built-in `collections` module to quickly tally up instances of 'A' versus 'B'. By iterating over multiple test cases through a loop defined by user input, this method ensures that comparisons are made accurately while maintaining performance standards required under tight computational resources[^3]. To further enhance execution speed when working with Python, consider submitting codes via platforms like PyPy instead of traditional interpreters whenever possible since they offer better runtime efficiencies especially important during competitive programming contests where milliseconds matter significantly.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值