#P8993. [北大集训 2021] 算术
[北大集训 2021] 算术
Background
CTT2021 D4T1.
Problem Description
Today, Xiao Q, who lives in a base- world, learned a method to determine whether a given big integer is a multiple of . We use as an example to describe this method. Let and . All operations in the method below are performed in base .
- From the least significant digit to the most significant digit, split the number into blocks, each consisting of consecutive digits. In the example, is split into three blocks: .
- From the least significant block to the most significant block, number each block starting from . In the example, block is , block is , and block is .
- Compute a value for block : let the value of block in base be . If is odd, then is the smallest non-negative integer such that . If is even, then is the smallest non-negative integer such that . In the example, , , and .
- Concatenate the values in order with larger indices in lower digits and smaller indices in higher digits, forming a base- number, and output it. In the example, the output is . It is easy to verify that both and are multiples of .
It can be proven that the input and output numbers produced by the method above are either both multiples of , or both not multiples of . Also, the number of digits becomes smaller, so by repeating this process several times you can obtain a very small number, and then you can check it easily.
Xiao Q was deeply attracted by this algorithm, so he wants to give a general method for different from . However, he found that when the values of change, in Step 1 is not necessarily : sometimes it is , sometimes it is greater than , and sometimes there is no that satisfies the requirement at all. Therefore, for given and , Xiao Q wants to know the minimum positive integer in Step 1 (in base ), such that no matter what the input is, the input and the corresponding output are either both multiples of , or both not multiples of , or report that such a does not exist.
Note that is not necessarily a prime number.
Input Format
There are multiple test cases in the testdata. It is guaranteed that within the same test point, is the same. The first line contains two positive integers , with and , representing the number of test cases and the parameter of the method.
The next lines each contain one integer , representing the base for each test case. It is guaranteed that .
All numbers in the input are given in decimal.
Output Format
For each test case, output one line. If no valid exists, output -1; otherwise, output the minimum positive integer that satisfies the condition.
2 9
14
16
2
-1
Hint
| Subtask ID | Score | ||
|---|---|---|---|
For the physical and mental health of contestants, the distributed file provides a big-integer modular multiplication function mul(A, B, P) implemented in down.cpp. You need to ensure , and the function will return . You may choose to use or not use this code freely. When calling it, you must ensure that are all no greater than .
Translated by ChatGPT 5