BZOJ3979 : [WF2012]infiltration

答案是$O(\log n)$级别的,故答案不超过6。

当答案是12345时,暴力枚举+压位检验即可,否则直接输出6。

时间复杂度$O(n^5)$。

#include<cstdio>
#define N 80
#define rep(i,n) for(int i=0;i<n;i++)
typedef unsigned int U;
int n,T;char s[N];
struct P{
  U x,y,z;
  P(){x=y=z=0;}
  P(U _x,U _y,U _z){x=_x,y=_y,z=_z;}
  inline P operator|(const P&b){return P(x|b.x,y|b.y,z|b.z);}
  inline void set(int p){
    if(p<32){x|=1U<<p;return;}
    p-=32;
    if(p<32){y|=1U<<p;return;}
    z|=1U<<(p-32);
  }
  inline int cnt(){return __builtin_popcount(x)+__builtin_popcount(y)+__builtin_popcount(z);}
}g[N],S;
inline bool one(){
  rep(i,n)if(g[i].cnt()==n)return 1;
  return 0;
}
inline bool two(){
  rep(i,n)rep(j,i)if((g[i]|g[j]).cnt()==n)return 1;
  return 0;
}
inline bool three(){
  rep(i,n)rep(j,i)rep(k,j)if((g[i]|g[j]|g[k]).cnt()==n)return 1;
  return 0;
}
inline bool four(){
  rep(i,n)rep(j,i)rep(k,j)rep(l,k)if((g[i]|g[j]|g[k]|g[l]).cnt()==n)return 1;
  return 0;
}
inline bool five(){
  rep(i,n)rep(j,i)rep(k,j)rep(l,k)rep(m,l)if((g[i]|g[j]|g[k]|g[l]|g[m]).cnt()==n)return 1;
  return 0;
}
int main(){
  while(~scanf("%d",&n)){
    printf("Case %d: ",++T);
    rep(i,n){
      scanf("%s",s);
      g[i]=P();
      g[i].set(i);
      rep(j,n)if(s[j]==‘1‘)g[i].set(j);
    }
    if(one()){puts("1");continue;}
    if(two()){puts("2");continue;}
    if(three()){puts("3");continue;}
    if(four()){puts("4");continue;}
    if(five()){puts("5");continue;}
    puts("6");
  }
  return 0;
}

  

时间: 2024-12-23 19:58:05

BZOJ3979 : [WF2012]infiltration的相关文章

bzoj 3978: [WF2012]Fibonacci Words

Description 斐波那契01字符串的定义如下 F(n) = { 0  if n = 0 1  if n = 1 F(n-1)+F(n-2) if n >= 2 } 这里+的定义是字符串的连接.F(n)的前几个元素如下: F(0)=0 F(1)=1 F(2)=10 F(3)=101 F(4)=10110 F(5)=10110101 F(6)=1011010110110 F(7)=101101011011010110101 F(8)=10110101101101011010110110101

幽门螺旋菌(14)_好处1

网址 http://europepmc.org/articles/PMC3733401;jsessionid=TTuOpoJQmfWbFgaULPVS.3 Nat Rev Microbiol. Author manuscript; available in PMC . Published in final edited form as: Nat Rev Microbiol. 11(6): 385–399. Published online . doi:  10.1038/nrmicro3016

Cryptographic method and system

The present invention relates to the field of security of electronic data and/or communications. In one form, the invention relates to data security and/or privacy in a distributed and/or decentralised network environment. In another form, the invent

Constructing continuous functions

This post summarises different ways of constructing continuous functions, which are introduced in Section 18 of James Munkres "Topology". Constant function. Inclusion function. N.B. The function domain should have the subspace topology relative