CSES - Aalto Competitive Programming 2024 - wk11 - Homework - Results
Submission details
Task:Finding inverse
Sender:snude
Submission time:2024-11-15 22:18:23 +0200
Language:C++ (C++11)
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 <algorithm>
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

// Debug printing
#ifdef DEBUG
#define deb(fmt, args...) printf("DEBUG: %d: " fmt, __LINE__, ##args)
#define debug_print(fmt, args...) printf(fmt, ##args)
#else
#define deb(fmt, args...)
#define debug_print(fmt, args...)
#endif

void print_array(vector<int> in, const string title = "Vector")
{
	debug_print("DEBUG: %s [\nDEBUG: ", title.c_str());
	for (unsigned int i = 0; i < in.size(); i++) {
		debug_print("%d ", in[i]);
	}
	debug_print("\nDEBUG: ] END\n");
}

void print_matrix(vector<vector<int> > in, const string title = "Matrix")
{
	debug_print("DEBUG: %s [\nDEBUG: ", title.c_str());
	for (unsigned int i = 0; i < in.size(); i++) {
		for (unsigned int j = 0; j < in[i].size(); j++) {
			debug_print("%d ", in[i][j]);
		}
		debug_print("\nDEBUG: ");
	}
	debug_print("DEBUG: ] END\n");
}

void factors(int n, vector<int> &f)
{
	f.clear();
	for (int i = 2; i * i < n; i++) {
		while (n % i == 0) {
			f.push_back(i);
			n /= i;
		}
	}
	if (n > 1)
		f.push_back(n);
}

ll exp(int a, int b)
{
	int mod = 1000000007;
	if (b <= 0)
		return 1;
	if (b == 1)
		return a;
	if (b & 1) {
		ll res = (a * exp(a, b - 1)) % mod;
		return res;
	} else {
		ll res = exp(a, b / 2);
		res = (res * res) % mod;
		return res;
	}
}

int gcd(int a, int b)
{
	if (b == 0)
		return a;
	return gcd(b, a % b);
}

ll totient(int n)
{
	// vector<int> f;
	ll res = 1;
	for (int i = 2; i * i <= n; i++) {
		int amount = 0;
		while (n % i == 0) {
			n /= i;
			amount++;
		}
		if (amount > 0) {
			res *= exp(i, amount - 1);
			res *= (i - 1);
		}
	}
	if (n > 1)
		res *= (n - 1);
	return res;
}

int main(int argc, char *argv[])
{
	ios::sync_with_stdio(0);
	cin.tie(0);

	// Read the input parameters
	int n, m;
	cin >> n >> m;

	if (gcd(n, m) != 1) {
		cout << -1 << "\n";
		return 0;
	}

	// x is the result of totient function
	
	// deb("%lld\n", totient(n));
	// deb("%lld\n", totient(m));

	int tot = totient(m);

	deb("tot: %d\n", tot);

	cout << exp(n, tot - 1) % m << "\n";

	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:

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