hdu3949(線性基,求第k小的異或和
阿新 • • 發佈:2018-09-12
鏈接 sel time cau int another logs 題目 there
Input
First line of the input is a single integer T(T<=30), indicates there are T test cases.
For each test case, the first line is an integer N(1<=N<=10000), 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(1<=Q<=10000), the number of queries. The fourth line contains Q numbers(each number is between 1 and 10^18) K1,K2,......KQ.
Output
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.
Sample Output
Case #1:
1
2
3
-1
Case #2:
0
1
2
3
-1
Hint
If you choose a single number, the result you get is the number you choose.
Using long long instead of int because of the result may exceed 2^31-1.
題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=3949
XOR
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4731 Accepted Submission(s): 1658
For each test case, the first line is an integer N(1<=N<=10000), 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(1<=Q<=10000), the number of queries. The fourth line contains Q numbers(each number is between 1 and 10^18) K1,K2,......KQ.
Sample Input 2 2 1 2 4 1 2 3 4 3 1 2 3 5 1 2 3 4 5
Author elfness
Source 2011 Multi-University Training Contest 11 - Host by UESTC
Recommend xubiao | We have carefully selected several similar problems for you: 3946 3948 3947 3945 3944 題目大意:輸入t,t組樣例,輸入n,有n個數,接下來為n個數的值,輸入q,代表q次詢問,接下來q個數,要你求出第k小的數 學習:https://www.cnblogs.com/vb4896/p/6149022.html 思路:這一題顯然是線性基來做,首先先把最大線性無關組,也就是線性基求出來,然後題目要求是要你求出第k小的數,在線性基的基礎上,我們可以稍微改造一下,就是保證只有b[i]的第i位是1,其它的第i 位都為0,有了這個性質,我們要求第k小的數,就是把k用二進制表示,如果第j為是1的話,就異或第j個線性基的向量,這裏可能需要的讀者多想一下,(因為都是二進制表示,具體的我也描述不出來) 看代碼:
#include<iostream> #include<string.h> #include<map> #include<cstdio> #include<cstring> #include<stdio.h> #include<cmath> #include<ctype.h> #include<math.h> #include<algorithm> #include<set> #include<queue> typedef long long ll; using namespace std; const ll mod=1e9+7; const int maxn=1e4+10; const int maxk=5e3+10; const int maxx=1e4+10; const ll maxe=1000+10; #define INF 0x3f3f3f3f3f3f #define Lson l,mid,rt<<1 #define Rson mid+1,r,rt<<1|1 ll a[maxn],b[100]; ll m,k; void guass(int n) { memset(b,0,sizeof(b)); for(int i=0;i<n;i++) { for(int j=63;j>=0;j--) { if((a[i]>>j)&1) { if(!b[j]) { b[j]=a[i]; break; } else { a[i]^=b[j]; } } } } for(int i=63;i>=0;i--) { if(!b[i]) continue; for(int j=i+1;j<=63;j++) { if((b[j]>>i)&1) b[j]^=b[i];//為了使得只有b[i]的第i為1,其它的都不為1 } } m=0; for(int i=0;i<=63;i++) if(b[i]) b[m++]=b[i]; } int main() { int t,ca=1,n,q; cin>>t; while(t--) { cin>>n; printf("Case #%d:\n",ca++); for(int i=0;i<n;i++) { cin>>a[i]; } guass(n); cin>>q; while(q--) { ll ans=0; cin>>k; if(n!=m) k--;//代表可以是0 if(k>=(1ll<<m)) cout<<"-1"<<endl; else { for(int i=0;i<=63;i++) if((k>>i)&1) ans^=b[i]; cout<<ans<<endl; } } } return 0; }
hdu3949(線性基,求第k小的異或和