Codeforces 878A - Short Program(位运算)

 

 

原题链接:http://codeforces.com/problemset/problem/878/A

 

题意:给出n个位运算操作, 化简这些操作, 使化简后的操作次数不多于5步。

 

思路:我们可以对二进制每一位上的1, 0, 进行讨论,

如果n次操作后1 -->1, 0 --> 1, 说明这一位要用或操作(or 1)

类似的,1 -->0, 0 -->1, 说明这一位要用异或操作(xor 1)

         1 -->0, 0 -->0, 说明这一位要用与操作(and 0)

              1 -->1, 0 -->0,前后不变,可以不用进行操作。

其中,and 0 操作可以用 or 1 xor 1代替, 那么最后化简后的操作次数不会超过 2 次。

 

AC代码:

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<iomanip>
 5 #include<algorithm>
 6 #include<string>
 7 using namespace std;
 8 typedef long long LL;    
 9 int XOR, OR;
10 int p[11];
11 int b1,b2;
12 char ch[500005];
13 int num[500005][11];
14 int cal(int b, int j, int i){
15     if(ch[j]=='|') return b|num[j][i];
16     if(ch[j]=='&') return b&num[j][i];
17     else return b^num[j][i];
18 }
19 int main()
20 {
21     int n,m;
22     p[0]=1;
23     for(int i=1;i<11;i++) p[i]=p[i-1]*2;
24     while(cin>>n)
25     {
26         XOR=OR=0;
27         memset(num, 0, sizeof(num));
28         for(int i=0;i<n;i++){
29             scanf(" %c %d", &ch[i], &m);
30             int t=0;
31             while(m){
32                 if(m&1) num[i][t]=1;
33                 t++;
34                 m>>=1;
35             }
36         }
37         for(int i=0;i<10;i++){
38             b1=1,b2=0;
39             for(int j=0;j<n;j++){
40                 b1=cal(b1, j, i);
41                 b2=cal(b2, j, i);
42             }
43             if(b1==0&&b2==0){//n and 1 相当于 n or 1 xor 1 
44                 OR^=p[i];
45                 XOR^=p[i];
46             }
47             if(b1==1&&b2==1) OR^=p[i];
48             if(b1==0&&b2==1) XOR^=p[i];
49         }
50         int sum=0;
51         if(OR) sum++;
52         if(XOR) sum++;
53         cout<<sum<<endl;
54         
55         if(OR) cout<<'|'<<' '<<OR<<endl;
56         if(XOR) cout<<'^'<<' '<<XOR<<endl; 
57     }
58 }

 

转载于:https://www.cnblogs.com/MasterSpark/p/7761060.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、付费专栏及课程。

余额充值