Problem p03586 - Generation 3

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 number of queries.
A list of Q tuples (A_i, M_i), where each tuple contains two integers:
- \( A_i \) (0 ≤ \( A_i \) ≤ 10^9)
- \( M_i \) (1 ≤ \( M_i \) ≤ 10^9)

### Example Input:
```
4
2 4
3 8
9 6
10 7
```

### Function Signature:
Write a function f(queries) that takes in the input.
```python
def f(queries: List[Tuple[int, int]]):
    '''
    queries: a list of tuples where each tuple contains two integers
    '''
```

Test Case Generator

Certainly! Below is a test case generator for the given problem statement. This generator will create random test cases based on the constraints provided.

```python
import random

def tcgen():
    Q = random.randint(1, 100)
    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

def print_test_case(Q, queries):
    print(Q)
    for A_i, M_i in queries:
        print(A_i, M_i)

# Example usage:
if __name__ == "__main__":
    Q, queries = tcgen()
    print_test_case(Q, queries)
```

The `tcgen` function generates a random number of queries \(Q\) (between 1 and 100). For each query, it picks random values for \(A_i\) (between 0 and \(10^9\)) and \(M_i\) (between 1 and \(10^9\)). The `print_test_case` function prints the generated test case in the required format.

You can run the script to generate and print a random test case. Here is an example of how the generated output might look:

```plaintext
7
12345678 98765432
0 1
999999999 1000000000
54321 1234567
10 7
177 168
2028 88772
```

This output corresponds to 7 queries with the respective pairs of \(A_i\) and \(M_i\).

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: 
#         inputs = extract_arguments(fh)
#         for A, M in inputs:
#             f(A, M)