「题解」「NOI2014」起床困难综合症

题目

传送门

题解

一道思路十分巧妙的题。

这道题与二进制有关,那么我们从分析二进制入手。

不难发现,二进制运算的每一位都是相互独立的,他们的运算不会影响到其他的数位。

那么我们可以分开运算每一位,把 \(0\)\(1\) 带入到运算当中,而结果将会有不同的情况

  • 假如经过运算之后,\(0\) 变成了 \(1\),也就是说如果初始攻击力的二进制下这一位为 \(0\),经过运算之后他会变成 \(1\),那么我们只需要让初始战力的这一位为 \(0\) 就好;
  • 假如运算之后,\(0\) 还是 \(0\)\(1\) 还是 \(1\),那么如果初始战力此位为 \(1\),运算之后它仍旧为 \(1\),但是这里需要判断我们计算出来的当前战力是否超过 \(m\) 的上界;

一共就这两种情况,而代码中因为每一个数位都是独立的,所以并没有每一位单独算,而是用 zero=0,one=(1ll<<31)-1 的情况将所有数位同时算。

并且,我们发现其实可以不需要算出初始战斗力,而可以直接算出最终结果。

代码

/*
* @Author: Arextre
* @Date:   2020-03-21 08:58:05
* @Last Modified by:   Arextre
* @Last Modified time: 2020-03-21 12:42:42
*/
#include<cstdio>

#define rep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i<=i##_end_;++i)
#define fep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i>=i##_end_;--i)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define ft first
#define sd second
#define LL long long
#define ull unsigned long long
#define uint unsigned int
#define pii pair< int,int >
#define Endl putchar('\n')
// #define FILEOI
// #define int long long
// #define int unsigned
// #define int unsigned long long

#ifdef FILEOI
# define MAXBUFFERSIZE 500000
inline char fgetc(){
    static char buf[MAXBUFFERSIZE+5],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXBUFFERSIZE,stdin),p1==p2)?EOF:*p1++;
}
# undef MAXBUFFERSIZE
# define cg (c=fgetc())
#else
# define cg (c=getchar())
#endif
template<class T>inline void qread(T& x){
    char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    if(f)x=-x;
}
inline int qread(){
    int x=0;char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    return f?-x:x;
}
// template<class T,class... Args>inline void qread(T& x,Args&... args){qread(x),qread(args...);}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline void getInv(int inv[],const int lim,const int MOD){
    inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
}
template<class T>void fwrit(const T x){
    if(x<0)return (void)(putchar('-'),fwrit(-x));
    if(x>9)fwrit(x/10);
    putchar(x%10^48);
}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
    return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
}

int n,m;

char op[5];int t;
int zero,one,ans;

signed main(){
#ifdef FILEOI
    freopen("file.in","r",stdin);
    freopen("file.out","w",stdout);
#endif
    scanf("%d %d",&n,&m);
    one=(1ll<<31)-1;
    rep(i,1,n){
        scanf("%s %d",op,&t);
        if(op[0]=='A')zero&=t,one&=t;
        else if(op[0]=='O')zero|=t,one|=t;
        else zero^=t,one^=t;
    }
    for(int i=31;i>=0;--i){
        if(zero&(1<<i))ans+=(1<<i);
        else if(one&(1<<i) && (1<<i)<=m)ans+=(1<<i),m-=(1<<i);
    }
    writc(ans,'\n');
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Arextre/p/12544038.html
今日推荐