Problem p03586 - Generation 2

Orig Description

Score : 1400 points
Problem StatementProcess the Q queries below.
You are given two integers A_i and M_i. Determine whether there exists a positive integer K_i not exceeding 2 × 10^{18} such that A_i^{K_i} ≡ K_i (mod M_i), and find one if it exists.
Constraints
1 \leq Q \leq 100
0 \leq A_i \leq 10^9(1 \leq i \leq Q)
1 \leq M_i \leq 10^9(1 \leq i \leq Q)
InputsInput is given from Standard Input in the following format:
Q
A_1 M_1
:
A_Q M_Q
OutputsIn the i-th line, print -1 if there is no integer K_i that satisfies the condition.
Otherwise, print an integer K_i not exceeding 2 × 10^{18} such that A_i^{K_i} ≡ K_i (mod M_i). If there are multiple solutions, any of them will be accepted.
Sample Input 14
2 4
3 8
9 6
10 7
Sample Output 14
11
9
2
It can be seen that the condition is satisfied: 2^4 = 16 ≡ 4 (mod 4), 3^{11} = 177147 ≡ 11 (mod 8), 9^9 = 387420489 ≡ 9 (mod 6) and 10^2 = 100 ≡ 2 (mod 7).
Sample Input 23
177 168
2028 88772
123456789 987654321
Sample Output 27953
234831584
471523108231963269

Extracted Specification

    
An integer Q (1 ≤ Q ≤ 100), representing some quantity or size.
A list of Q tuples, where each tuple contains two integers (A, M):
- The first integer A is between 0 and 10^9.
- The second integer M is between 1 and 10^9.

### Example Input:

```
4
2 4
3 8
9 6
10 7
```

### Function Signature:

Write a function `f(inputs)` that takes in the input.

```python
def f(inputs: List[Tuple[int, int]]):
    '''
    inputs: a list of tuples, where each tuple contains two integers (A, M)
    '''
```

Test Case Generator

Certainly! Here is the test case generator for the given problem statement:

```python
import random

def tcgen():
    # Number of queries
    Q = random.randint(1, 100)
    
    queries = []
    
    for _ in range(Q):
        # Ai can be 0 or any integer up to 10^9
        A_i = random.randint(0, 10**9)
        # Mi can be any integer from 1 to 10^9
        M_i = random.randint(1, 10**9)
        queries.append((A_i, M_i))
        
    return Q, queries

# Example of how to use the generated test case
Q, queries = tcgen()
print(Q)
for A_i, M_i in queries:
    print(A_i, M_i)
```

In the test case generator:

1. `Q` is randomly chosen between 1 and 100.
2. For each query, `A_i` is randomly chosen between 0 and \(10^9\).
3. `M_i` is randomly chosen between 1 and \(10^9\). 

The function returns `Q` and the list of queries, each containing a tuple `(A_i, M_i)`.

To see how the test case generator works, you can run the `tcgen` function and print the values.

Extract Arguments

def extract_arguments(fh):
    Q = int(fh.readline().strip())
    queries = []
    for _ in range(Q):
        A, M = map(int, fh.readline().strip().split())
        queries.append((A, M))
    return queries

# if __name__ == "__main__":
#     input_path = sys.argv[1]
#     with open(input_path, 'r') as fh:
#         queries = extract_arguments(fh)
#         for A, M in queries:
#             f(A, M)