Codeforces Round #768 (Div. 2)
B. Fun with Even Subarrays
You are given an array of elements. You can apply the following operation to it any number of times:
Select some subarray from a of even size that begins at position and for each between and (inclusive), assign the value to .
For example, if , then choose and , applying this operation the array will become .
Find the minimum number of operations (possibly zero) needed to make all the elements of the array equal.
Input
The input consists of multiple test cases. The first line contains a single integer ) — the number of test cases. Description of the test cases follows.
The first line of each test case contains an integer — the length of the array.
The second line of each test case consists of integers — the elements of the array .
It is guaranteed that the sum of n over all test cases does not exceed .
Output
Print lines, each line containing the answer to the corresponding test case — the minimum number of operations needed to make equal all the elements of the array with the given operation.
Example
input
output
Note
In the first test, all elements are equal, therefore no operations are needed.
In the second test, you can apply one operation with and , set , and the array becomes with operation.
In the third test, you can apply one operation with and , set , and the array becomes .
In the fourth test, you can apply one operation with and , set , and the array becomes , then you can apply another operation with and , set , and the array becomes .
In the fifth test, there is only one element, therefore no operations are needed.
解题思路
思维
每次都是从后面的数字往前面复制相同长度的一段,可知最后数组中相等的元素一定是最后一个数字,可先找出最后数字相等的那段,然后像滚雪球一样往前滚,注意其中有些可能是等于最后一个数字的,这些可以跳过
- 时间复杂度:
代码
C. And Matching
You are given a set of ( is always a power of ) elements containing all integers exactly once.
Find pairs of elements such that:
Each element in the set is in exactly one pair.
The sum over all pairs of the bitwise AND of its elements must be exactly equal to . Formally, if and are the elements of the pair, then the following must hold:
,
where denotes the bitwise AND operation.
If there are many solutions, print any of them, if there is no solution, print instead.
Input
The input consists of multiple test cases. The first line contains a single integer — the number of test cases. Description of the test cases follows.
Each test case consists of a single line with two integers and ( is a power of , ).
The sum of over all test cases does not exceed $2^{16}. All test cases in each individual input will be pairwise different.
Output
For each test case, if there is no solution, print a single line with the integer .
Otherwise, print lines, the $i-th4 of them must contain and , the elements in the pair.
If there are many solutions, print any of them. Print the pairs and the elements in the pairs in any order.
Example
input
output
Note
In the first test,
In the second test,
In the third test,
In the fourth test, there is no solution.
解题思路
构造
首先需要注意 的取值范围,任意两个数的运算之和不止 ,对于 不妨考虑分类讨论:
-
时,让首尾两个数依次组成一对
-
时,在上面的基础上,让 跟 组成一对, 跟 组成一对
-
时,
-
- 时无解
-
- 否则,让 跟 , 跟 , 跟 组成一对
-
时间复杂度:
代码
__EOF__

本文链接:https://www.cnblogs.com/zyyun/p/15851909.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!