1934.Codeforces Round 931 (Div. 2) - sol

20240301

Codeforces Round 931 (Div. 2)


A. Too Min Too Max

Given an array \(a\) of \(n\) elements, find the maximum value of the expression:

\[|a_i - a_j| + |a_j - a_k| + |a_k - a_l| + |a_l - a_i| \]

where \(i\), \(j\), \(k\), and \(l\) are four distinct indices of the array \(a\), with \(1 \le i, j, k, l \le n\).

Here \(|x|\) denotes the absolute value of \(x\).

还愣住了。


发现并不是每一个数都要和别的数做差,比如 \(a_i\)\(a_k\) 就不会对答案造成贡献。

所以一种非常贪心又简单的想法,我让 \(a_i,a_k\) 是整个序列的最大和次大,\(a_j,a_l\) 是最小和次小,这样就一定可以满足条件了。

这样我们得到的答案是最大的,于是就做完了。代码


B. Yet Another Coin Problem

You have \(5\) different types of coins, each with a value equal to one of the first \(5\) triangular numbers: \(1\), \(3\), \(6\), \(10\), and \(15\). These coin types are available in abundance. Your goal is to find the minimum number of these coins required such that their total value sums up to exactly \(n\).

We can show that the answer always exists.

\(1 \le n \le 10^9\)

赛时继续寄。


单纯地想回发现,是不是先用 \(15\) 再在剩下用 \(10\),再这样继续递减下去。

但是你会发现过不了样例。

这时就可以分析一下,发现其实只会对于 \(\le 30\)\(n\) 会有所例外,而当 \(n \gt 30\) 时我们一定有一种最优方案是一直用 \(15\) 一直用到小于 \(30\),因为他们的 \(lcm\)\(30\),非常好证明。

那么我们可以稍微枚举一下,会发现当 \(n=12,20,23,27\) 时是不满足我们的贪心策略的,

于是可以直接特判一下即可。代码(不建议参考,还用 dp 预处理了一些)。


C. Find a Mine

This is an interactive problem.

You are given a grid with \(n\) rows and \(m\) columns. The coordinates \((x, y)\) represent the cell on the grid, where \(x\) (\(1 \leq x \leq n\)) is the row number counting from the top and \(y\) (\(1 \leq y \leq m\)) is the column number counting from the left. It is guaranteed that there are exactly \(2\) mines in the grid at distinct cells, denoted as \((x_1, y_1)\) and \((x_2, y_2)\). You are allowed to make no more than \(4\) queries to the interactor, and after these queries, you need to provide the location of one of the mines.

In each query, you can choose any grid cell \((x, y)\), and in return, you will receive the minimum Manhattan distance from both the mines to the chosen cell, i.e., you will receive the value \(\min(|x-x_1|+|y-y_1|, |x-x_2|+|y-y_2|)\).

Your task is to determine the location of one of the mines after making the queries.

\(2 \le n,m \le 10^8\)

又是一道交互题。


首先,只能询问 \(4\) 次挺不正常的,这可能是突破口。(建议画图)

随机撒点去询问一定不现实,而只能询问 \(4\) 次,那么我们就希望每次询问找到固定的四个点去问?

画图发现,我们可以从整个网格的四个角开始。

而这四个角得到的答案,一定是两个相邻的角对应一个地雷,并且只要我知道这两个距离,就可以得到地雷的位置了。

直接把距离之和与 \(n/m\) 作差就可以得到 \(x/y\) 坐标,这是画图容易发现的。


那么现在就需要知道每个角对应的是哪一个地雷了。

由于对应一个地雷的一定是相邻的两个角,所以我们可以先询问 \((1,1)\)\((1,m)\)

假设他们两个对应的是同一个地雷,那么就可以得到这个地雷的具体位置 \((x,y)\),于是只要再通过一次询问,询问 \((x,y)\) 是否真正有地雷即可。

如果有,即返回 \(0\) 直接输出;反之,我们就知道了 \((1,1)\)\((n,1)\) 是确定的一个地雷,于是再询问 \((1,n)\) 就可以得到地雷的位置了,这样就做完了。代码


D1. XOR Break — Solo Version

This is the solo version of the problem. Note that the solution of this problem may or may not share ideas with the solution of the game version. You can solve and get points for both versions independently.

You can make hacks only if both versions of the problem are solved.

Given an integer variable \(x\) with the initial value of \(n\). A single break operation consists of the following steps:

  • Choose a value \(y\) such that \(0 \lt y \lt x\) and \(0 \lt (x \oplus y) \lt x\).
  • Update \(x\) by either setting \(x = y\) or setting \(x = x \oplus y\).

Determine whether it is possible to transform \(x\) into \(m\) using a maximum of \(63\) break operations. If it is, provide the sequence of operations required to achieve \(x = m\).

You don't need to minimize the number of operations.

Here \(\oplus\) denotes the bitwise XOR operation.

\(1 \le m \lt n \le 10^{18}\)

讨论不周到提前下播了。


感觉被 \(k \le 63\) 害了。

给出这个条件,第一反应就是去拆位,对于每一维去讨论让他满足条件。

但发现完全不用,只要 \(m\oplus n \lt n\) 你直接一次操作就可以完成了(这里我们知道 \(m\)\(m \oplus n\) 其实是等价的,两者是可以互换的,所以一下讨论都是会将 \(n\) 变成 \(m\))。

那什么时候不满足条件呢?

容易发现,当 \(m\) 的第一位在 \(n\) 的那一位为 \(0\) 时,异或起来就会大,比如说 \((110)_2\)\((1)_2\)

但是这种情况就是没有救的吗?

我们发现只要这一位前面在 \(n\) 中存在至少两个 \(1\) ,那么我们就可以通过一步转化改变局面,

即通过把最高位的 \(1\) 去掉,把后面的变成 \(2^k -1\)

比如刚才那种情况,我们可以把 \((110)_2\) 变成 \((011)_2\),这样就可以满足条件了。

而当前面只有一个 \(1\) 时呢?发现这就是完成不了的,因为当你想把前面的 \(1\) 消去时,\(m\) 又会 \(\gt n\) 了。


这样我们就做完了,其实只需要最多两步操作啊。代码


D2. XOR Break — Game Version

This is an interactive problem.

This is the game version of the problem. Note that the solution of this problem may or may not share ideas with the solution of the solo version. You can solve and get points for both versions independently.

Alice and Bob are playing a game. The game starts with a positive integer \(n\), with players taking turns. On each turn of the game, the following sequence of events takes place:

  • The player having the integer \(p\) breaks it into two integers \(p_{1}\) and \(p_{2}\), where \(0 \lt p_{1} \lt p\), \(0 \lt p_{2} \lt p\) and \(p_{1} \oplus p_{2} = p\).
  • If no such \(p_{1}\), \(p_{2}\) exist, the player loses.
  • Otherwise, the opponent does either select the integer \(p_{1}\) or \(p_{2}\).
  • The game continues with the selected integer. The opponent will try to break it.

As Alice, your goal is to win. You can execute a maximum of \(63\) break operations. You have the choice to play first or second. The system will act for Bob.

Here \(\oplus\) denotes the bitwise XOR operation.

\(1 \le n \le 10^{18}\)

简单题,可惜赛时没看下播了。


这种博弈题,一看就一定存在必胜策略,于是你可以直接写一个 dp 看看,感觉就和二进制 \(1\) 的个数相关。

因为是 \(p_1 \oplus p_2 = p\) 所以在 \(p\)\(1\) 的位置,\(p_1,p_2\) 有且仅有一个为 \(1\),反之两者相同。

那么容易发现,当 \(p\) 只有一个 \(1\) 时,一定是先手必败的,推广到 \(p\) 是奇数个 \(1\),同样是先手必败;

而当 \(p\) 有偶数个 \(1\) 时,拿出来一个,就只会剩下奇数个 \(1\) 是先手必胜的。


于是这样我们每一次操作都拿出一个 \(1\) 来,给对方留下奇数个 \(1\),那么我们是必胜的。

最后通过二进制 \(1\) 的个数奇偶性稍微判断一下自己是先手还是后手就可以了,实现非常简单。

样例居然是错的。 代码


E. Weird LCM Operations

Given an integer \(n\), you construct an array \(a\) of \(n\) integers, where \(a_i = i\) for all integers \(i\) in the range \([1, n]\). An operation on this array is defined as follows:

  • Select three distinct indices \(i\), \(j\), and \(k\) from the array, and let \(x = a_i\), \(y = a_j\), and \(z = a_k\).
  • Update the array as follows: \(a_i = \operatorname{lcm}(y, z)\), \(a_j = \operatorname{lcm}(x, z)\), and \(a_k = \operatorname{lcm}(x, y)\), where \(\operatorname{lcm}\) represents the least common multiple.

Your task is to provide a possible sequence of operations, containing at most \(\lfloor \frac{n}{6} \rfloor + 5\) operations such that after executing these operations, if you create a set containing the greatest common divisors (GCDs) of all subsequences with a size greater than \(1\), then all numbers from \(1\) to \(n\) should be present in this set.

After all the operations \(a_i \le 10^{18}\) should hold for all \(1 \le i \le n\).

We can show that an answer always exists.

\(3 \le n \le 3 \times 10^4\)

好题。


首先,发现限制 \(\lfloor \frac{n}{6} \rfloor\) 就非常奇怪,这个怎么解释呢?

每次操作三个数,全部操作完也需要 \(\lfloor \frac{n}{3} \rfloor\) 次,而这样的限制是不是只要操作一半。

大胆猜测一下,分析发现确实好像是这样的。

发现对于 \(\le \frac{n}{2}\) 的数,他们在这里面一定是存在倍数的,于是我们通过 \(gcd(x,2x)\) 就可以得到 \(x\)

而其他的数却不能得到,所以我们只需要对后一半的数讨论。


再来分析一下,感觉一个数被操作两次是不现实的,第一是这样次数会超,第二,这样的 gcd 会不会变大,从而就失去效果了?

那么我们尝试用只操作一次的策略完成,先从满足 \(n,n-1,n-2\) 开始。

  • \(n\) 是奇数的时候,他们三个一定是互质的,那么我们就直接操作 \((n,n-1,n-2)\),这样会得到 \((n(n-1),(n-1)(n-2),n(n-2))\),而两两做 gcd,就可以满足条件。

  • \(n\) 是偶数时,\(n\)\(n-2\) 就会有公因子 \(2\),这是不利的,所以我们考虑把其中一个 \(\div 2\)

    • \(n\)\(4\) 的倍数时,\(\frac{n}{2}\) 也是偶数,是不满足条件的,所以我们考虑用 \(\frac{n-2}{2}\),操作 \((\frac{n-2}{2},n-1,n)\)
    • \(n\) 不是 \(4\) 的倍数,则 \(\frac{n}{2}\) 是奇数,我们就直接操作 \((\frac{n}{2},n-1,n-2)\) 即可。

    这样的操作可以刚好在做 gcd 时用上 \(2\) 这个公因子,从而可以得到这四个数。

按照这样的操作,我们三个三个地处理就是可以完成的。

而如果 \(\frac{n}{2}\) 不是 \(3\) 的倍数怎么办?

发现我们可以用上 \(1\),先操作一次 \((1,n-1,n)\) 就可以满足条件了(因为算不算 \(\lfloor \frac{n}{2}\rfloor\) 都是不影响的,所以 \(\equiv 2/1 \pmod 3\) 都是一种情况)。

这样就做完了,代码也是好写的,感觉突破口就在于对限制的大胆猜想。代码


Conclusion

  1. 对于每道题的限制要加以分析,可能会成为突破口,但如果有更优的策略,也不要被限制误导了。(D1,E)
  2. 有关 gcd,lcm 的题目一定要尽量构造 互质 的情况。(E)
posted @ 2024-03-03 17:25  H_W_Y  阅读(44)  评论(0编辑  收藏  举报