1. 程式人生 > >jzoj5991. 【北大2019冬令營模擬2019.1.6】Juice

jzoj5991. 【北大2019冬令營模擬2019.1.6】Juice

題面

題解

好迷……

//minamoto
#include<bits/stdc++.h>
#define R register
#define ll long long
#define fp(i,a,b) for(R int i=a,I=b+1;i<I;++i)
#define fd(i,a,b) for(R int i=a,I=b-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
using namespace std;
char buf[1<<21],*p1=buf,*p2=buf;
inline char getc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
int read(){
    R int res,f=1;R char ch;
    while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
    for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');
    return res*f;
}
char sr[1<<21],z[20];int C=-1,Z=0;
inline void Ot(){fwrite(sr,1,C+1,stdout),C=-1;}
void print(R int x){
    if(C>1<<20)Ot();if(x<0)sr[++C]='-',x=-x;
    while(z[++Z]=x%10+48,x/=10);
    while(sr[++C]=z[Z],--Z);sr[++C]='\n';
}
const int N=(1<<20)+5;
int a[25],sz[N],n,lim,ans;ll sum,s[N],b[N];
bool ck(int m){
    fp(i,1,n)b[i]=1ll*m*a[i]-sum;
    fp(S,1,lim-1){
        s[S]=0;
        fp(i,0,n-1)if(S&(1<<i))s[S]+=b[i+1];
    }
    int S=lim-1;
    fp(i,1,n)if(b[i]==0)S^=1<<(i-1);
    while(true){
        int flag=1;
        for(R int i=S;i;i=(i-1)&S)if(s[i]==-sum){
            S^=i,flag=0;break;
        }if(flag)return false;
        if(!S)break;
    }return true;
}
int main(){
//  freopen("testdata.in","r",stdin);
    freopen("juice.in","r",stdin);
    freopen("juice.out","w",stdout);
    n=read(),lim=(1<<n);
    fp(i,1,n)a[i]=read(),sum+=a[i];
    fp(i,1,n)if(ck(i))return printf("%d\n",i),0;
    return 0;
}