正文
增加 3 行删除 3 行
1234511 行未改动17181920212223242510 行未改动3637383940414243446 行未改动感谢同校大佬 @[Huxuanjie0517](https://www.luogu.com.cn/user/795779) 提供的[基础思路](https://www.luogu.com.cn/discuss/1103642),这位大佬同时还是本题[最优解](https://www.luogu.com.cn/record/223643722)(旧数据)的保持者/bx。这是本做法的代码:```cpp#include<bits/stdc++.h>using namespace std;11 行未改动cout << ans;return 0;}```核心思路是 $a\ \text{and}\ b \le \min(a,b)$,这样可以避免朴素 $O(n^2)$ 做法中很多不必要的枚举。但这个做法被 [hack](https://www.luogu.com.cn/record/242955732) 了,于是我们尝试再进行优化。- - -注意到 hack 掉此代码数据的 $a$ 类似于`1 2 3 4 5...`,这可以使得每一次枚举的 $ans$ 都小于 $a_i$,为了破坏 $a$ 的这一特点,我们想到了随机化。本题中 $a$ 的顺序与答案无关,所以考虑随机重排 $a$,来破坏 $a$ 递增的这一性质。10 行未改动cin >> n;for(int i = 1;i <= n;i++){cin >> a[i];}shuffle(a + 1,a + n + 1,rnd);for(int i = 1;i <= n;i++){if(a[i] <= ans)continue;for(int j = 1;j < i;j++){ans = max(ans,a[i] & a[j]);6 行未改动1234511 行未改动17181920212223242510 行未改动3637383940414243446 行未改动感谢同校大佬 @[Huxuanjie0517](https://www.luogu.com.cn/user/795779) 提供的[基础思路](https://www.luogu.com.cn/discuss/1103642)。这是本做法的代码:```cpp#include<bits/stdc++.h>using namespace std;11 行未改动cout << ans;return 0;}```核心思路是 $x\ \text{and}\ y \le \min(x,y)$,这样可以避免朴素 $O(n^2)$ 做法中很多不必要的枚举。但这个做法被 [hack](https://www.luogu.com.cn/record/242955732) 了,于是我们尝试再进行优化。- - -注意到 hack 掉此代码数据的 $a$ 类似于`1 2 3 4 5...`,这可以使得每一次枚举的 $ans$ 都小于 $a_i$,为了破坏 $a$ 的这一特点,我们想到了随机化。本题中 $a$ 的顺序与答案无关,所以考虑随机重排 $a$,来破坏 $a$ 递增的这一性质。10 行未改动cin >> n;for(int i = 1;i <= n;i++){cin >> a[i];}shuffle(a + 1,a + n + 1,rnd);//打乱序列for(int i = 1;i <= n;i++){if(a[i] <= ans)continue;for(int j = 1;j < i;j++){ans = max(ans,a[i] & a[j]);6 行未改动