Submission details
Task:Finding inverse
Sender:aalto26kh_001
Submission time:2026-11-12 11:24:12 +0200
Language:Python3 (PyPy3)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.05 sdetails
#2ACCEPTED0.05 sdetails
#3ACCEPTED0.05 sdetails
#4ACCEPTED0.05 sdetails
#5ACCEPTED0.05 sdetails
#6ACCEPTED0.05 sdetails
#7ACCEPTED0.05 sdetails
#8ACCEPTED0.05 sdetails
#9ACCEPTED0.05 sdetails
#10ACCEPTED0.05 sdetails
#11ACCEPTED0.05 sdetails
#12ACCEPTED0.05 sdetails
#13ACCEPTED0.05 sdetails
#14ACCEPTED0.05 sdetails

Code

# FULL TEMPLATE: delete every block you don't use. Blocks don't depend on each other.
# Nodes are 1..n (slot 0 unused). Arrays are 0-indexed.
import sys
from collections import deque, defaultdict, Counter
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
from itertools import accumulate, permutations, combinations, product
from functools import lru_cache, cmp_to_key
from math import gcd, isqrt, comb, inf
input = sys.stdin.readline
MOD = 10**9 + 7

def I():  return int(input())                    # "5"          -> 5
def MI(): return map(int, input().split())       # "8 4"        -> n, q = MI()
def LI(): return list(map(int, input().split())) # "3 2 4 5"    -> [3, 2, 4, 5]
def S():  return input().strip()                 # "BABAZA", ".*.#" (kept as text)
def LS(): return input().split()                 # "? 2 3"      -> ['?', '2', '3']


# ==== UNION-FIND ==== uf.join(a, b) -> False if already together, uf.size(x)
class UF:
    def __init__(self, n):
        self.e = [-1] * n

    def find(self, x):
        e = self.e
        while e[x] >= 0:
            if e[e[x]] >= 0:
                e[x] = e[e[x]]
            x = e[x]
        return x

    def size(self, x):
        return -self.e[self.find(x)]

    def join(self, a, b):
        a, b = self.find(a), self.find(b)
        if a == b:
            return False
        if self.e[a] > self.e[b]:
            a, b = b, a
        self.e[a] += self.e[b]
        self.e[b] = a
        return True


# ==== FENWICK ==== ft.add(i, d): a[i] += d.  ft.query(i) = a[0] + ... + a[i-1]
class FT:
    def __init__(self, n):
        self.s = [0] * n

    def add(self, i, d):
        s, n = self.s, len(self.s)
        while i < n:
            s[i] += d
            i |= i + 1

    def query(self, i):
        s, r = self.s, 0
        while i > 0:
            r += s[i - 1]
            i &= i - 1
        return r


# ==== SEGMENT TREE (min) ==== t.update(i, v): a[i] = v.  t.query(l, r) = min of a[l..r-1]
# sum/max: change INF and the 3 lines marked "op"
class Tree:
    INF = inf

    def __init__(self, a):
        n = self.n = len(a)
        t = self.t = [self.INF] * n + list(a)
        for i in range(n - 1, 0, -1):
            t[i] = min(t[2 * i], t[2 * i + 1])         # op

    def update(self, i, v):
        t = self.t
        i += self.n
        t[i] = v
        i >>= 1
        while i:
            t[i] = min(t[2 * i], t[2 * i + 1])         # op
            i >>= 1

    def query(self, l, r):
        t, res = self.t, self.INF
        l += self.n
        r += self.n
        while l < r:
            if l & 1:
                res = min(res, t[l])                    # op
                l += 1
            if r & 1:
                r -= 1
                res = min(res, t[r])                    # op
            l >>= 1
            r >>= 1
        return res


# ==== BFS (unweighted graph) ==== -> dist[v], -1 = unreachable
def bfs(adj, start):
    dist = [-1] * len(adj)
    dist[start] = 0
    q = deque([start])
    while q:
        u = q.popleft()
        for v in adj[u]:
            if dist[v] == -1:
                dist[v] = dist[u] + 1
                q.append(v)
    return dist


# ==== DIJKSTRA ==== wadj[u] = [(v, w)] -> dist[v], inf = unreachable
def dijkstra(wadj, start):
    dist = [inf] * len(wadj)
    dist[start] = 0
    pq = [(0, start)]
    while pq:
        d, u = heappop(pq)
        if d != dist[u]:
            continue
        for v, w in wadj[u]:
            if d + w < dist[v]:
                dist[v] = d + w
                heappush(pq, (dist[v], v))
    return dist


# ==== GRID BFS ==== g[r][c], '#' = wall -> dist[r][c], -1 = unreachable
DIRS = ((-1, 0), (1, 0), (0, -1), (0, 1))              # U D L R
def grid_bfs(g, sr, sc):
    R, C = len(g), len(g[0])
    dist = [[-1] * C for _ in range(R)]
    dist[sr][sc] = 0
    q = deque([(sr, sc)])
    while q:
        r, c = q.popleft()
        for dr, dc in DIRS:
            nr, nc = r + dr, c + dc
            if 0 <= nr < R and 0 <= nc < C and g[nr][nc] != '#' and dist[nr][nc] == -1:
                dist[nr][nc] = dist[r][c] + 1
                q.append((nr, nc))
    return dist


# ==== BINARY SEARCH ON THE ANSWER ==== smallest x in [lo, hi] with ok(x) True (ok: False...False True...True)
def first_true(lo, hi, ok):
    while lo < hi:
        mid = (lo + hi) // 2
        if ok(mid):
            hi = mid
        else:
            lo = mid + 1
    return lo


# mod power is built in: pow(b, e, MOD).  inverse: pow(a, MOD - 2, MOD)


def main():
    a, m = MI()

    if m == 1:
        print(-1)  # no x satisfies 0 < x < 1
        return
    try:
        print(pow(a, -1, m))
    except ValueError:
        print(-1)


main()

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: ACCEPTED

input
184120 505187

correct output
438779

user output
438779

Test 10

Verdict: ACCEPTED

input
264601 885661

correct output
360221

user output
360221

Test 11

Verdict: ACCEPTED

input
40310 590135

correct output
-1

user output
-1

Test 12

Verdict: ACCEPTED

input
202254499 577081420

correct output
128866679

user output
128866679

Test 13

Verdict: ACCEPTED

input
539836073 888851205

correct output
797044652

user output
797044652

Test 14

Verdict: ACCEPTED

input
697847215 756971670

correct output
-1

user output
-1