B. Gardener and the Array
B. Gardener and the Array
The gardener Kazimir Kazimirovich has an array of integers .
He wants to check if there are two different subsequences and of the original array, for which , where is the bitwise OR of all of the numbers in the sequence .
A sequence is a subsequence of if can be obtained from by deleting several (possibly none or all) elements.
Two subsequences are considered different if the sets of indexes of their elements in the original sequence are different, that is, the values of the elements are not considered when comparing the subsequences.
Input
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
The first line of each test case contains one integer () — the size of the array .
The description of the array in this problem is given implicitly to speed up input.
The -st of the following lines of the test case begins with an integer () — the number of set bits in the number . Next follow distinct integers () —the numbers of bits that are set to one in number . In other words, .
It is guaranteed that the total sum of in all tests does not exceed .
Output
For each set of input, print "Yes" if there exist two different subsequences for which , and "No" otherwise.
You can output the answer in any case (upper or lower). For example, the strings "", "", "", and "" will be recognized as positive responses.
Example
input
5 3 2 1 5 2 2 4 2 2 3 2 2 1 2 1 2 4 3 1 2 4 2 2 4 4 1 2 5 6 2 2 5 5 3 3 1 2 3 2 5 3 5 7 2 3 1 4 5 1 2 6 3 5 3 2 6 3 2 1 1 1 2
output
No
Yes
Yes
Yes
No
Note
It can be proven that in the first test case there are no two different subsequences and for which .
In the second test case, one of the possible answers are following subsequences: the subsequence formed by the element at position , and the subsequence formed by the elements at positions and .
In the third test case, one of the possible answers are following subsequences: the subsequence formed by elements at positions , , and , and the subsequence formed by elements at positions , and .
解题思路
感觉官方给出的题解写得不是很好,给出的证明跟没证一样。
首先容易想到,如果存在两个数各自在二进制下为的位所构成的集合属于包含关系,例如在二进制下为的位所构成的集合为,对应的集合为,那么存在包含关系,为了方便这里标记为。那么我们就可以构造出一组解,其中子数组取全集(即全部的数),子数组取(这里假设),很明显且(这里的指的是数的集合)。
这就证明了如果存在两个数的二进制位属于包含关系,那么有解。上面证明了充分性,题解只通过充分性就推出“如果不存在两个数的二进制位属于包含关系,那么无解”,很明显逻辑有问题,因此还要证明这个命题的必要性,即“如果有解,那么存在两个数的二进制位属于包含关系”,从而根据逆否命题推出等价命题“如果不存在两个数的二进制位属于包含关系,那么无解”。
如果有解,那么存在子数组和满足且。那么有
其中保证和。
通过上面的推导可以得到,并且由于,因此(反证法,如果,由于,而,因此,即,矛盾),意味着至少存在两个数的二进制位属于包含关系。这是因为将被包含的数从全集中删除,得到真子集,同时还满足。必要性得到证明。
因此得到证明:如果存在两个数的二进制位属于包含关系 有解,从而可以推出逆否命题:如果不存在两个数的二进制位属于包含关系,那么无解。
因此接下来只需要开个哈希表枚举统计每个数在二进制下为的位出现的次数,然后再枚举每个数,看看该数能否被其他数包含(等价于看看该数在二进制下所有是的位在哈希表中出现次数是否都大于等于),如果是那么就存在解。如果全部数都无法被其他数包含那么就无解。
AC代码如下:
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int N = 1e5 + 10; 5 6 vector<int> p[N]; 7 8 void solve() { 9 int n; 10 scanf("%d", &n); 11 unordered_map<int, int> cnt; 12 for (int i = 0; i < n; i++) { 13 int m; 14 scanf("%d", &m); 15 p[i].clear(); 16 while (m--) { 17 int x; 18 scanf("%d", &x); 19 cnt[x]++; 20 p[i].push_back(x); 21 } 22 } 23 for (int i = 0; i < n; i++) { 24 bool flag = true; 25 for (auto &x : p[i]) { 26 if (cnt[x] < 2) { 27 flag = false; 28 break; 29 } 30 } 31 if (flag) { 32 printf("Yes\n"); 33 return; 34 } 35 } 36 printf("No\n"); 37 } 38 39 int main() { 40 int t; 41 scanf("%d", &t); 42 while (t--) { 43 solve(); 44 } 45 46 return 0; 47 }
参考资料
Codeforces Round #843 (Div. 2) Editorial:https://codeforces.com/blog/entry/111286
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/17053892.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效