Task: | Finding inverse |
Sender: | hungdojan |
Submission time: | 2024-11-17 19:25:20 +0200 |
Language: | C++ (C++17) |
Status: | READY |
Result: | WRONG ANSWER |
test | verdict | time | |
---|---|---|---|
#1 | ACCEPTED | 0.00 s | details |
#2 | ACCEPTED | 0.00 s | details |
#3 | ACCEPTED | 0.00 s | details |
#4 | ACCEPTED | 0.00 s | details |
#5 | ACCEPTED | 0.00 s | details |
#6 | ACCEPTED | 0.00 s | details |
#7 | ACCEPTED | 0.00 s | details |
#8 | ACCEPTED | 0.00 s | details |
#9 | WRONG ANSWER | 0.00 s | details |
#10 | WRONG ANSWER | 0.00 s | details |
#11 | ACCEPTED | 0.00 s | details |
#12 | WRONG ANSWER | 0.00 s | details |
#13 | WRONG ANSWER | 0.00 s | details |
#14 | ACCEPTED | 0.00 s | details |
Code
#include <bits/stdc++.h> using namespace std; #define I_2D(row, col, width) ((row) * (width) + (col)) #define PRINT_ARR(arr, n) \ do { \ for (int i = 0; i < n; i++) { \ cout << arr[i] << " "; \ } \ cout << "\n"; \ } while (0) #define PRINT_VEC_ARR(v, n) \ do { \ for (int i = 0; i < n; i++) { \ cout << i << ": "; \ for (auto item : v[i]) { \ cout << item << " "; \ } \ cout << endl; \ } \ } while (0) #define endl '\n'; typedef long long ll; int _gcd(int a, int b) { if (b < 1) { return a; } return _gcd(b, a % b); } int modpow(int a, int b, int m) { // a^b % m int res = (b & 1) ? a : 1; b >>= 1; for (; b; b >>= 1) { a = (a * a) % m; if (b & 1) { res = (res * a) % m; } } return res; } int euler_totient_fnc(int n) { int res = 1; for (int i = 2; i * i <= n; i++) { int curr = 1; int count = -1; while (n % i == 0) { count++; if (count > 0) { curr *= i; } n /= i; } if (count >= 0) { curr *= (i - 1); } res *= curr; } if (n > 1) { res *= n - 1; } return res; } int inverse_modulo(int a, int m) { int euler_totient = euler_totient_fnc(m) - 1; return modpow(a, euler_totient, m); } int main() { ios::sync_with_stdio(0); cin.tie(0); int a, m; cin >> a >> m; if (_gcd(a, m) != 1) { cout << -1 << endl; return 0; } cout << inverse_modulo(a, m) << 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: WRONG ANSWER
input |
---|
184120 505187 |
correct output |
---|
438779 |
user output |
---|
-92661 |
Test 10
Verdict: WRONG ANSWER
input |
---|
264601 885661 |
correct output |
---|
360221 |
user output |
---|
174467 |
Test 11
Verdict: ACCEPTED
input |
---|
40310 590135 |
correct output |
---|
-1 |
user output |
---|
-1 |
Test 12
Verdict: WRONG ANSWER
input |
---|
202254499 577081420 |
correct output |
---|
128866679 |
user output |
---|
-505430217 |
Test 13
Verdict: WRONG ANSWER
input |
---|
539836073 888851205 |
correct output |
---|
797044652 |
user output |
---|
783066715 |
Test 14
Verdict: ACCEPTED
input |
---|
697847215 756971670 |
correct output |
---|
-1 |
user output |
---|
-1 |