CSES - Aalto Competitive Programming 2024 - wk11 - Homework - Results
Submission details
Task:Finding inverse
Sender:Rasse
Submission time:2024-11-14 14:00:00 +0200
Language:C++ (C++17)
Status:READY
Result:
Test results
testverdicttime
#1--details
#20.00 sdetails
#30.00 sdetails
#40.02 sdetails
#50.00 sdetails
#6--details
#70.08 sdetails
#8ACCEPTED0.01 sdetails
#9--details
#10--details
#11--details
#12--details
#13--details
#14--details

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 sign(v) ((0 < (v)) - ((v) < 0))

int p(int a, int b, int m)
{
    if (b == 0) return 1;
    else if (b % 2 == 0) { int r = p(a, b/2, m); return (r*r) % m; }
    else return (p(a, b-1, m)*a) % m; 
}
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, m) * (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();
        cout.flush();
    }
}

Test details

Test 1

Verdict:

input
6 7

correct output
6

user output
(empty)

Test 2

Verdict:

input
0 7

correct output
-1

user output
(empty)

Test 3

Verdict:

input
5 78

correct output
47

user output
-1-19511203817901
-19511203817901
-19511203817901
-19511203817901

Test 4

Verdict:

input
89 99

correct output
89

user output
1
-19511203817901
-19511203817901
-19511203817901
-19511203817901
...
Truncated

Test 5

Verdict:

input
0 61

correct output
-1

user output
(empty)

Test 6

Verdict:

input
897 947

correct output
625

user output
(empty)

Test 7

Verdict:

input
419 538

correct output
217

user output
-1-19511203817901
-19511203817901
-19511203817901
-19511203817901
-19511203817901
...
Truncated

Test 8

Verdict: ACCEPTED

input
32 938

correct output
-1

user output
-1-19511203817901
-19511203817901
-19511203817901
-19511203817901
-19511203817901
...
Truncated

Test 9

Verdict:

input
184120 505187

correct output
438779

user output
(empty)

Test 10

Verdict:

input
264601 885661

correct output
360221

user output
(empty)

Test 11

Verdict:

input
40310 590135

correct output
-1

user output
(empty)

Test 12

Verdict:

input
202254499 577081420

correct output
128866679

user output
(empty)

Test 13

Verdict:

input
539836073 888851205

correct output
797044652

user output
(empty)

Test 14

Verdict:

input
697847215 756971670

correct output
-1

user output
(empty)