CSES - Aalto Competitive Programming 2024 - wk11 - Homework - Results
Submission details
Task:Finding inverse
Sender:arnxxau
Submission time:2024-11-17 18:45:07 +0200
Language:C++ (C++20)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.00 sdetails
#4ACCEPTED0.00 sdetails
#5ACCEPTED0.00 sdetails
#6ACCEPTED0.00 sdetails
#7ACCEPTED0.00 sdetails
#8ACCEPTED0.00 sdetails
#9ACCEPTED0.00 sdetails
#10ACCEPTED0.00 sdetails
#11ACCEPTED0.00 sdetails
#12ACCEPTED0.00 sdetails
#13ACCEPTED0.00 sdetails
#14ACCEPTED0.00 sdetails

Code

#include <iostream>
using namespace std;

// Función para calcular el GCD y los coeficientes x, y usando el Algoritmo Extendido de Euclides
int extended_gcd(int a, int b, int &x, int &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    int x1, y1;
    int gcd = extended_gcd(b, a % b, x1, y1);
    x = y1;
    y = x1 - (a / b) * y1;
    return gcd;
}

// Función para encontrar el inverso modular
int modular_inverse(int a, int M) {
    int x, y;
    int gcd = extended_gcd(a, M, x, y);
    if (gcd != 1) {
        return -1; // No existe el inverso modular si gcd(a, M) != 1
    }
    x = (x % M + M) % M; // Asegurar que x esté en el rango 0 < x < M
    return x;
}

int main() {
    int a, M;
    cin >> a >> M;

    int result = modular_inverse(a, M);
    cout << result << endl;

    return 0;
}

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