#abc228d. D - Linear Probing
D - Linear Probing
Score : points
问题描述
存在一个包含 项的序列 。最初,每个项的值均为 。
按照顺序处理 个查询请求。第 ()个查询由一个整数 和另一个整数 描述,其中 可以为 或 ,具体如下:
- 若 ,按以下顺序执行操作:
- 定义一个整数 ,令其等于 。
- 当 时,持续向 中加 。根据本题的约束条件,可以证明这个过程在有限次迭代后结束。
- 将 的值替换为 。
- 若 ,打印当时 的值。
这里,对于整数 和 , 表示 除以 后的余数。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
There is a sequence with terms. Initially, every term is .
Process queries in order. The -th query is described by an integer such that or , and another integer , as follows.
- If , do the following in order.
- Define an integer as .
- While , keep adding to . We can prove that this process ends after finite iterations under the Constraints of this problem.
- Replace the value of with .
- If , print the value of at that time.
Here, for integers and , denotes the remainder when is divided by .
Constraints
- There is at least one such that .
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
For each query with , print the response in one line. It is guaranteed that there is at least one such query.
Sample Input 1
4
1 1048577
1 1
2 2097153
2 3
Sample Output 1
1048577
-1
We have , so the first query sets .
In the second query, initially we have , for which , so we add to . Now we have , so this query sets .
In the third query, we print .
In the fourth query, we print .
Note that, in this problem, is a constant and not given in input.
update @ 2024/3/10 09:58:52