CSES - Aalto Competitive Programming 2024 - wk11 - Homework - Results
Submission details
Task:Finding inverse
Sender:Rasse
Submission time:2024-11-14 13:48:14 +0200
Language:C++ (C++17)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#30.00 sdetails
#40.00 sdetails
#5ACCEPTED0.00 sdetails
#60.00 sdetails
#70.00 sdetails
#8ACCEPTED0.00 sdetails
#90.00 sdetails
#100.00 sdetails
#11ACCEPTED0.00 sdetails
#120.00 sdetails
#130.00 sdetails
#14ACCEPTED0.00 sdetails

Code

#include <iostream>
#include <vector>
#include <array>
#include <string>
#include <algorithm>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <climits>
#include <cmath>
#include <functional>
#include <type_traits>
#include <fstream>
#include <bitset>
#include <complex>
#include <iomanip>
 
#define int long long
using namespace std;
 
#define cross(x, y) (((x)*conj(y)).imag())
#define mag(x) (sqrt(x.imag() * x.imag() + x.real() * x.real())
#define sign(v) ((0 < (v)) - ((v) < 0))

unordered_map<string, long double> mp;

int mod = 1e9+7;

int p(int a, int b)
{
    if (b == 0) return 1;
    else if (b % 2 == 0) { int r = p(a, b/2); return (r*r) % mod; }
    else return (p(a, b-1)*a) % mod; 
}
int gdc(int a, int b)
{
    if (b == 0) return a;
    return gdc(b, a%b);
}

void solve()
{
    int a, m;
    cin >> a >> m;

    if (gdc(a, m) != 1)
    {
        cout << -1;
        return;
    }

    unordered_map<int, int> factors;
    int num = m;
    int top = sqrt(num);
    for (int i = 2; i <= top && i < num; i++)
    {
        while (num % i == 0)
        {
            factors[i]++;
            num /= i;
        }
    }
    if (num > 1)
        factors[num]++;

    // Calc phi
    int phi = 1;
    for (auto kv : factors)
        phi *= p(kv.first, kv.second-1) * (kv.first - 1);

    cout << (p(a, phi-1) % m) << '\n';
}
 
 
signed main()
{
    ios::sync_with_stdio(0);
    cout.precision(25);
    //setprecision(10);
    //cin.tie(0);
 
    int t = 1;
 
    //cin >> t;
    //ifstream f("testcase.txt");
    //cin.rdbuf(f.rdbuf());
 
    for (int i = 0; i < t; i++)
        solve();
}

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:

input
5 78

correct output
47

user output
37

Test 4

Verdict:

input
89 99

correct output
89

user output
70

Test 5

Verdict: ACCEPTED

input
0 61

correct output
-1

user output
-1

Test 6

Verdict:

input
897 947

correct output
625

user output
55

Test 7

Verdict:

input
419 538

correct output
217

user output
212

Test 8

Verdict: ACCEPTED

input
32 938

correct output
-1

user output
-1

Test 9

Verdict:

input
184120 505187

correct output
438779

user output
483354

Test 10

Verdict:

input
264601 885661

correct output
360221

user output
714959

Test 11

Verdict: ACCEPTED

input
40310 590135

correct output
-1

user output
-1

Test 12

Verdict:

input
202254499 577081420

correct output
128866679

user output
334944642

Test 13

Verdict:

input
539836073 888851205

correct output
797044652

user output
467544261

Test 14

Verdict: ACCEPTED

input
697847215 756971670

correct output
-1

user output
-1