NC51055. XOR
描述
输入描述
First line of the input is a single integer, indicates there are T test cases.
For each test case, the first line is an integer, the number of numbers below. The second line contains N integers (each number is between 1 and 10^18). The third line is a number Q
, the number of queries. The fourth line contains Q numbers(each number is between 1 and
) K1,K2,......KQ.
输出描述
For each test case,first output Case #C: in a single line,C means the number of the test case which is from 1 to T. Then for each query, you should output a single line contains the Ki-th smallest number in them, if there are less than Ki different numbers, output -1.
示例1
输入:
2 2 1 2 4 1 2 3 4 3 1 2 3 5 1 2 3 4 5
输出:
Case #1: 1 2 3 -1 Case #2: 0 1 2 3 -1
说明:
If you choose a single number, the result you get is the number you choose.C++(g++ 7.5.0) 解法, 执行用时: 100ms, 内存消耗: 5992K, 提交时间: 2022-08-04 17:21:38
#include<bits/stdc++.h> #define ll long long #define rep(i,a,b) for(int i=a;i<=b;i++) #define rep2(i,a,b) for(int i=a;i>=b;i--) using namespace std; const int maxn=100010; ll p[66],x; int main() { int T,N,Q,Cas=0; scanf("%d",&T); while(T--){ scanf("%d",&N); rep(i,0,63) p[i]=0; rep(i,1,N) { scanf("%lld",&x); rep2(j,63,0){ if(x&(1LL<<j)){ if(p[j]) x^=p[j]; else { p[j]=x;break;} } } } ll num=0,ans,K; rep(i,0,63) if(p[i]){ p[num++]=p[i]; rep(j,i+1,63) if((p[j]>>i)&1) p[j]^=p[i]; } scanf("%d",&Q); printf("Case #%d:\n",++Cas); while(Q--){ scanf("%lld",&K); if(N!=num) K--; if(K>=(1LL<<num)) puts("-1"); else { ans=0; rep(j,0,63) { if(K&(1LL<<j)) ans^=p[j]; } printf("%lld\n",ans); } } } return 0; }
C++ 解法, 执行用时: 377ms, 内存消耗: 2468K, 提交时间: 2022-01-01 14:09:18
#include<bits/stdc++.h> using namespace std; long long a[10001],bb[66]={1},ans,k; int n,m,T,t; int main(){ for(int i=1;i<=60;i++)bb[i]=bb[i-1]<<1; cin>>T; for(int c=1;c<=T;c++){ bool lg=0; cin>>n; for(int i=1;i<=n;i++)cin>>a[i]; t=n; for(int i=1;i<=n;i++){ for(int j=i+1;j<=n;j++)if(a[j]>a[i])swap(a[j],a[i]); if(!a[i]){t=i-1;lg=1;break;} for(int k=60;k>=0;k--)if(a[i]&bb[k]){ for(int j=1;j<=n;j++)if(j!=i&&(a[j]&bb[k]))a[j]^=a[i]; break; } } cin>>m; cout<<"Case #"<<c<<":\n"; while(m--){ cin>>k; k-=lg; if(k>=bb[t]){cout<<"-1\n";continue;} ans=0; for(int i=t-1;i>=0;i--)if(k&bb[i])ans^=a[t-i]; cout<<ans<<"\n"; } } return 0; }