D. Binary Spiders
time limit per test 2 seconds
memory limit per test 256 megabytes
input standard input
output standard output
Binary Spiders are species of spiders that live on Mars. These spiders weave their webs to defend themselves from enemies.
To weave a web, spiders join in pairs. If the first spider in pair has x x x legs, and the second spider has y y y legs, then they weave a web with durability x ⊕ y x⊕y x⊕y. Here, ⊕ ⊕ ⊕ means bitwise XOR.
Binary Spiders live in large groups. You observe a group of n n n spiders, and the i i i-th spider has ai legs.
When the group is threatened, some of the spiders become defenders. Defenders are chosen in the following way. First, there must be at least two defenders. Second, any pair of defenders must be able to weave a web with durability at least k k k. Third, there must be as much defenders as possible.
Scientists have researched the behaviour of Binary Spiders for a long time, and now they have a hypothesis that they can always choose the defenders in an optimal way, satisfying the conditions above. You need to verify this hypothesis on your group of spiders. So, you need to understand how many spiders must become defenders. You are not a Binary Spider, so you decided to use a computer to solve this problem.
Input
The first line contains two integers
n
n
n and
k
k
k
(
2
≤
n
≤
3
⋅
1
0
5
,
0
≤
k
≤
2
30
−
1
)
(2≤n≤3⋅10^5, 0≤k≤2^{30}−1)
(2≤n≤3⋅105,0≤k≤230−1), the amount of spiders in the group and the minimal allowed durability of a web.
The second line contains n n n integers a i ( 0 ≤ a i ≤ 2 30 − 1 ) a_i (0≤a_i≤2^{30}−1) ai(0≤ai≤230−1) — the number of legs the i i i-th spider has.
Output
In the first line, print a single integer
ℓ
(
2
≤
ℓ
≤
n
)
ℓ (2≤ℓ≤n)
ℓ(2≤ℓ≤n), the maximum possible amount of defenders.
In the second line, print ℓ ℓ ℓ integers b i b_i bi, separated by a single space ( 1 ≤ b i ≤ n ) (1≤b_i≤n) (1≤bi≤n) — indices of spiders that will become defenders.
If there exists more than one way to choose the defenders, print any of them.
Unfortunately, it may appear that it’s impossible to choose the defenders. In this case, print a single integer −1.
Examples
input
6 8
2 8 4 16 10 14
output
3
1 5 4
input
6 1024
1 2 3 1 4 0
output
-1
题解:首先要知道 n n n个数中任选2个数异或的最小值一定等于:将这 n n n个数排序后,所有相邻2个数的异或值中的最小值。
如何证明呢?我只需要证明从 n n n个数中取任意3个正整数 a , b , c ( a < b < c ) a,b,c(a<b<c) a,b,c(a<b<c)(等于的情况不需考虑),均满足 a ⨁ b < a ⨁ c a\bigoplus b<a\bigoplus c a⨁b<a⨁c 或者 b ⨁ c < a ⨁ c b\bigoplus c<a\bigoplus c b⨁c<a⨁c,即一定存在相邻2个数异或值小于不相邻的2个数的异或值。
假设 a , c a,c a,c的第 i i i个bit位分别为 a i , c i a_i,c_i ai,ci,那么从高位枚举至低位,当 a k a_k ak第一次不等于 c k c_k ck时, a k = 0 , c k = 1 a_k=0,c_k=1 ak=0,ck=1,若此时 b k = 0 b_k=0 bk=0那么就有 a ⨁ b < a ⨁ c a\bigoplus b<a\bigoplus c a⨁b<a⨁c;若 b k = 1 b_k=1 bk=1那么就有 b ⨁ c < a ⨁ c b\bigoplus c<a\bigoplus c b⨁c<a⨁c,至此证明完成。
回到题目,有了上面的结论,我们可将
a
a
a从小到大排序,用
d
i
d_i
di表示以
a
i
a_i
ai为最大值结尾的最大集合大小,则
d
i
=
m
a
x
(
d
j
+
1
)
(
a
i
⨁
a
j
≥
k
)
d_i=max(d_j+1)(a_i\bigoplus a_j\geq k)
di=max(dj+1)(ai⨁aj≥k),暴力转移复杂度为
O
(
n
2
)
O(n^2)
O(n2),考虑用Trie树加速,从Trie树中找出满足
a
i
⨁
a
j
≥
k
a_i\bigoplus a_j\geq k
ai⨁aj≥k的路径,并取出路径伤的值更新
d
i
d_i
di,然后将
a
i
a_i
ai插入Trie
中并将
d
i
d_i
di更新至插入的路径中。
#include<bits/stdc++.h>
#define mid (l+r)/2
#define pii pair<int,int>
using namespace std;
const long long INF=-1e18;
const int MAX=6e6+10;
const int MOD=998244353;
typedef long long ll;
int d[MAX],f[MAX],pre[MAX],son[MAX][2],all=1;
pii a[MAX];
int main()
{
int n,k;
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)scanf("%d",&a[i].first);
for(int i=1;i<=n;i++)a[i].second=i;
sort(a+1,a+n+1);
for(int i=1;i<=n;i++)
{
int ans=0,root=1;
for(int j=30;j>=0&&root;j--)
{
int l=(a[i].first&(1<<j))>0;
int r=son[root][l^1];
if(k&(1<<j))root=r;
else
{
if(d[ans]<d[f[r]])ans=f[r];
root=son[root][l];
}
if(j==0&&d[ans]<d[f[root]])ans=f[root];
}
d[i]=d[ans]+1;
pre[i]=ans;
root=1;
for(int j=30;j>=0;j--)
{
int t=(a[i].first&(1<<j))>0;
if(son[root][t]==0)son[root][t]=++all;
root=son[root][t];
if(d[f[root]]<d[i])f[root]=i;
}
}
int i=max_element(d+1,d+n+1)-d;
if(d[i]<2)return 0*puts("-1");
printf("%d\n",d[i]);
for(;i!=0;i=pre[i])printf("%d ",a[i].second);
return 0;
}