本文主要是介绍codeforces 878 A short programme,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
老毕教我了思路,然而比赛时还时没敲完,赛后debug15分钟ac
思路因为所有位运算的数小于2的10次方,所以我们把每一此操作对应到每一位上,然后对不必要的操作化简,比如说 (这里都是指每一位的)&0 |1 == | 1 , | 1 & 0 == & 0.。。
注意 ^1^1 = 无操作。。
#include <bits/stdc++.h>
using namespace std ;
typedef long long ll ;
const int maxn = 500050 ;
int save[maxn][2] ;
bool vis[maxn] ;
int k[20] ;
int fuhao[20] ;
///codeforce 878A
int main()
{int n ;char read[10] ;while(~scanf("%d" , &n) ){int a , b;k[1] = 1 ;for(int i = 2 ; i <= 10 ; i ++ ){k[i] = k[i-1] << 1 ;}memset(fuhao , 0 , sizeof(fuhao)) ;/// |1 1/// &0 2/// ^0 3/// ^1 4for(int i = 0 ; i < n ; i ++ ){scanf("%s %d" , read , &save[i][1]) ;save[i][0] = read[0] ;a = save[i][1] ;if(save[i][0] == '|'){b = 1 ;while(a){if(a & 1) fuhao[b] = 1 ;a >>= 1 ;b ++ ;}}else if(save[i][0] == '^'){b = 1 ;while(a){int t = (a & 1) ? 1 : 0 ;if(fuhao[b] == 1 && t == 1){fuhao[b] = 2 ;}else if(fuhao[b] == 1 && t == 0){fuhao[b] = 1 ;}else if(fuhao[b] == 2 && t == 1 ){fuhao[b] = 1 ;}else if(fuhao[b] == 2 && t == 0 ){fuhao[b] = 2 ;}else if(fuhao[b] == 3 && t == 1){fuhao[b] = 4 ;}else if(fuhao[b] == 3 && t == 0){fuhao[b] = 0 ;}else if(fuhao[b] == 4 && t == 1 ){fuhao[b] = 0 ;}else if(fuhao[b] == 4 && t == 0 ){fuhao[b] = 4 ;}else if(fuhao[b] == 0){if(t == 0) fuhao[b] = 3 ;else fuhao[b] = 4 ;}a >>= 1 ;b ++ ;}}else{b = 1 ;for(int j = 0 ; j < 10 ; j ++){if(!(a & 1)){fuhao[j+1] = 2 ;}a >>= 1 ;}}//for(int i = 1 ; i <= 10 ; i ++ ) cout << fuhao[i] << " " ; cout << endl ;}//for(int i = 1 ; i <= 10 ; i ++ ) cout << fuhao[i] << " " ; cout << endl ;int yu = 0 , huo = 0 , yihuo = 0 ;for(int i = 10 ; i >= 1 ; i -- ){if(fuhao[i] != 2){yu |= 1 ;}yu <<= 1 ;if(fuhao[i] == 1){huo |= 1 ;}huo <<= 1 ;if(fuhao[i] == 4 ){yihuo |= 1 ;}yihuo <<= 1 ;}yu >>= 1 ;huo >>= 1 ;yihuo >>= 1 ;int num = 3 ;if(yu == 1023){num -- ;}if(huo == 0) num -- ;if(yihuo == 0) num -- ;printf("%d\n" , num);//printf("& %d\n| %d\n^ %d\n" , yu , huo , yihuo ) ;if(yu != 1023) printf("& %d\n" , yu) ;if(huo != 0) printf("| %d\n" , huo) ;if(yihuo != 0 ) printf("^ %d\n" , yihuo ) ;//int ans = 16 ;/*for(int i = 0 ; i < n ; i ++ ){if(save[i][0] == '|'){ans |= save[i][1] ;}else if(save[i][0] == '^'){ans ^= save[i][1] ;}else{ans &= save[i][1] ;}cout << ans << endl ;}*/}return 0;
}
这篇关于codeforces 878 A short programme的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!