NC51087. XOR
描述
输入描述
The input consists of several test cases and is terminated by end-of-file.
The first line of each test case contains an integer n.
The second line contains n integers .
*
*
* The number of test cases does not exceed 100.
* The sum of n does not exceed .
输出描述
For each test case, print an integer which denotes the result.
示例1
输入:
1 0 3 1 2 3 2 1000000000000000000 1000000000000000000
输出:
1 3 2
C++14(g++5.4) 解法, 执行用时: 1633ms, 内存消耗: 1248K, 提交时间: 2019-07-20 10:26:54
#include<bits/stdc++.h> using namespace std; typedef long long ll; const ll mod=1e9+7; int n,w; ll v,ans; ll a[100010]; ll qpow(ll x,ll y) { ll ans=1; for(;y;y>>=1,x=x*x%mod) if(y&1) ans=ans*x%mod; return ans; } struct base{ ll r[64],o[64]; bool ins(ll x){ bool flag=0; ll tmp=0; for(int i=62;i>=0;i--) if(x>>i){ if(!r[i]){ o[i]=tmp|(1ll<<i); r[i]=x; flag=1; } x^=r[i]; tmp^=o[i]; if(!x) break; } if(!flag) v|=tmp; return flag; } void clear(){ for(int i=0;i<64;i++){ r[i]=0; o[i]=0; } } }f; int main() { while(cin>>n){ for(int i=1;i<=n;i++) cin>>a[i]; f.clear(); v=0; ans=0; w=0; for(int i=1;i<=n;i++) ans+=!f.ins(a[i]); for(int i=0;i<64;i++){ if(v&(1ll<<i)) ans++; if(f.r[i]) w++; } cout<<ans*qpow(2,n-w-1)%mod<<endl; } return 0; }
C++11(clang++ 3.9) 解法, 执行用时: 1136ms, 内存消耗: 4504K, 提交时间: 2019-07-21 16:56:54
#include<bits/stdc++.h> using namespace std;const int N=1e5+7,mod=1e9+7;typedef long long ll; struct LB{ ll a[65]; void init(){memset(a,0,sizeof(a));} bool ins(ll x){ for(int i=62;i>=0;--i)if(x>>i&1) if(!a[i]){a[i]=x;return true;}else x^=a[i]; return false; } }t1,t2,t3;vector<int>d;int n,i,j,k,r,ans,bin[N*2];ll a[N],b[N],c[N]; int main(){ for(bin[0]=1,i=1;i<N*2;++i)bin[i]=bin[i-1]*2%mod; for(;~scanf("%d",&n);r=ans=0,t2.init(),t1.init()){ for(i=1;i<=n;++i){ scanf("%lld",a+i); if(t1.ins(a[i]))c[++r]=a[i];else b[++ans]=a[i]; } if(n==r){puts("0");continue;} for(i=1;i<=ans;++i)t2.ins(b[i]); for(i=1;i<=r;++i){ for(j=1,t3=t2;j<=r;++j)if(i!=j)t3.ins(c[j]); if(!t3.ins(c[i]))ans++; }printf("%lld\n",1LL*ans*bin[n-r-1]%mod); } }