CSES - Aalto Competitive Programming 2024 - wk11 - Homework - Results
Submission details
Task:Finding inverse
Sender:snude
Submission time:2024-11-15 19:32:19 +0200
Language:C++ (C++11)
Status:READY
Result:
Test results
testverdicttime
#10.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 <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)
{
	// vector<pair<int, int>> f;
	f.clear();
	for (int i = 2; i * i < n; i++) {
		// int sum = 0;
		while (n % i == 0) {
			// sum++;
			f.push_back(i);
			n /= i;
		}
		// if (sum > 0) {
		// 	f.push_back({i, sum});
		// }
	}
	if (n > 1)
		f.push_back(n);
}

ll count(int a, int b)
{
	int mod = 1000000007;
	if (b <= 0)
		return 1;
	if (b == 1)
		return a;
	if (b & 1) {
		ll res = (a * count(a, b - 1)) % mod;
		return res;
	} else {
		ll res = count(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);
}

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;
	}

	vector<int> f;
	factors(n, f);
	sort(f.begin(), f.end());

	int num = f[0];
	int amount = 0;
	ll x = 1;
	for (unsigned int i = 0; i < f.size(); i++) {
		if (f[i] == num) {
			amount++;
		} else {
			x *= count(num, amount - 1);
			x *= num - 1;
			num = f[i];
			amount = 1;
		}
	}
	x *= count(num, amount - 1);
	x *= num - 1;

	cout << x << "\n";

	return 0;
}

Test details

Test 1

Verdict:

input
6 7

correct output
6

user output
2

Test 2

Verdict: ACCEPTED

input
0 7

correct output
-1

user output
-1

Test 3

Verdict:

input
5 78

correct output
47

user output
4

Test 4

Verdict:

input
89 99

correct output
89

user output
88

Test 5

Verdict: ACCEPTED

input
0 61

correct output
-1

user output
-1

Test 6

Verdict:

input
897 947

correct output
625

user output
528

Test 7

Verdict:

input
419 538

correct output
217

user output
418

Test 8

Verdict: ACCEPTED

input
32 938

correct output
-1

user output
-1

Test 9

Verdict:

input
184120 505187

correct output
438779

user output
73632

Test 10

Verdict:

input
264601 885661

correct output
360221

user output
264600

Test 11

Verdict: ACCEPTED

input
40310 590135

correct output
-1

user output
-1

Test 12

Verdict:

input
202254499 577081420

correct output
128866679

user output
197550864

Test 13

Verdict:

input
539836073 888851205

correct output
797044652

user output
446040000

Test 14

Verdict: ACCEPTED

input
697847215 756971670

correct output
-1

user output
-1