Problem p03586 - Generation 1

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

Canonicalized Input Description:

An integer Q (1 ≤ Q ≤ 100), representing some quantity or size.
A list of Q tuples, each containing two integers (A, M):
- 0 ≤ A ≤ 10^9
- 1 ≤ M ≤ 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
    '''
```

Test Case Generator

Sure, here is a test case generator for the given problem statement:

```python
import random

def tcgen():
    # Generate the number of queries
    Q = random.randint(1, 100)
    
    # Generate the list of (A_i, M_i) pairs
    queries = []
    for _ in range(Q):
        A_i = random.randint(0, 10**9)
        M_i = random.randint(1, 10**9)
        queries.append((A_i, M_i))
    
    return Q, queries

# Example usage
Q, queries = tcgen()
print(Q)
for A_i, M_i in queries:
    print(A_i, M_i)
```

Explanation:
- The number of queries \( Q \) is randomly chosen between 1 and 100.
- For each query, \( A_i \) is randomly chosen between 0 and \( 10^9 \), and \( M_i \) is randomly chosen between 1 and \( 10^9 \).
- The queries are returned as a list of tuples.

This generator will produce test cases that fit within the constraints of the problem statement.

Extract Arguments

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

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