Task: | Finding inverse |
Sender: | esya_rae |
Submission time: | 2024-11-18 12:08:01 +0200 |
Language: | Python3 (PyPy3) |
Status: | READY |
Result: | TIME LIMIT EXCEEDED |
test | verdict | time | |
---|---|---|---|
#1 | ACCEPTED | 0.04 s | details |
#2 | ACCEPTED | 0.04 s | details |
#3 | ACCEPTED | 0.04 s | details |
#4 | ACCEPTED | 0.04 s | details |
#5 | ACCEPTED | 0.04 s | details |
#6 | ACCEPTED | 0.05 s | details |
#7 | ACCEPTED | 0.04 s | details |
#8 | ACCEPTED | 0.05 s | details |
#9 | TIME LIMIT EXCEEDED | -- | details |
#10 | TIME LIMIT EXCEEDED | -- | details |
#11 | TIME LIMIT EXCEEDED | -- | details |
#12 | TIME LIMIT EXCEEDED | -- | details |
#13 | TIME LIMIT EXCEEDED | -- | details |
#14 | TIME LIMIT EXCEEDED | -- | details |
Code
import sys sys.setrecursionlimit(10**6) input = sys.stdin.readline def sieve_primes(n): primes = [] for i in range(2, n // 2 + 1): is_prime = True for j in primes: if i % j == 0: is_prime = False if is_prime: primes.append(i) return primes def euler(n, primes): div = [] for p in primes: if n % p == 0: div.append(p) # print(div) if not div: return n - 1 c = n for d in div: c *= (d - 1) c //= d return c def fast_exp(a, b, mod): if b == 0: return 1 g = 1 while b > 1: if b % 2 == 1: g = (g * a) % mod b -= 1 a = (a * a) % mod b //= 2 return (a * g) % mod a, M = map(int, input().split()) e = euler(M, sieve_primes(M)) # print(e) r = fast_exp(a, e - 1, M) if (r * a) % M == 1: print(r) else: print(-1)
Test details
Test 1
Verdict: ACCEPTED
input |
---|
6 7 |
correct output |
---|
6 |
user output |
---|
6 |
Test 2
Verdict: ACCEPTED
input |
---|
0 7 |
correct output |
---|
-1 |
user output |
---|
-1 |
Test 3
Verdict: ACCEPTED
input |
---|
5 78 |
correct output |
---|
47 |
user output |
---|
47 |
Test 4
Verdict: ACCEPTED
input |
---|
89 99 |
correct output |
---|
89 |
user output |
---|
89 |
Test 5
Verdict: ACCEPTED
input |
---|
0 61 |
correct output |
---|
-1 |
user output |
---|
-1 |
Test 6
Verdict: ACCEPTED
input |
---|
897 947 |
correct output |
---|
625 |
user output |
---|
625 |
Test 7
Verdict: ACCEPTED
input |
---|
419 538 |
correct output |
---|
217 |
user output |
---|
217 |
Test 8
Verdict: ACCEPTED
input |
---|
32 938 |
correct output |
---|
-1 |
user output |
---|
-1 |
Test 9
Verdict: TIME LIMIT EXCEEDED
input |
---|
184120 505187 |
correct output |
---|
438779 |
user output |
---|
(empty) |
Test 10
Verdict: TIME LIMIT EXCEEDED
input |
---|
264601 885661 |
correct output |
---|
360221 |
user output |
---|
(empty) |
Test 11
Verdict: TIME LIMIT EXCEEDED
input |
---|
40310 590135 |
correct output |
---|
-1 |
user output |
---|
(empty) |
Test 12
Verdict: TIME LIMIT EXCEEDED
input |
---|
202254499 577081420 |
correct output |
---|
128866679 |
user output |
---|
(empty) |
Test 13
Verdict: TIME LIMIT EXCEEDED
input |
---|
539836073 888851205 |
correct output |
---|
797044652 |
user output |
---|
(empty) |
Test 14
Verdict: TIME LIMIT EXCEEDED
input |
---|
697847215 756971670 |
correct output |
---|
-1 |
user output |
---|
(empty) |