| Task: | Finding inverse |
| Sender: | aalto26kh_016 |
| Submission time: | 2026-11-18 16:35:43 +0200 |
| Language: | C++ (C++20) |
| Status: | READY |
| Result: | ACCEPTED |
| 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 | ACCEPTED | 0.00 s | details |
| #10 | ACCEPTED | 0.00 s | details |
| #11 | ACCEPTED | 0.00 s | details |
| #12 | ACCEPTED | 0.00 s | details |
| #13 | ACCEPTED | 0.00 s | details |
| #14 | ACCEPTED | 0.00 s | details |
Compiler report
input/code.cpp: In member function 'bool compute::start()':
input/code.cpp:38:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long unsigned int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for (int i=0;i<af.size();i++) {
| ~^~~~~~~~~~
input/code.cpp:39:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long unsigned int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for (int j=0;j<Mf.size();j++) {
| ~^~~~~~~~~~
input/code.cpp: In member function 'uint64_t compute::totient(std::vector<long unsigned int>&)':
input/code.cpp:82:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long unsigned int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | for ( int i=1;i<v.size();i++){
| ~^~~~~~~~~
input/code...Code
#include <iostream>
#include <vector>
#include <climits>
#include <cstdint>
using namespace std;
#define DEBUG std::cout<<"LINE"<<__LINE__;
#define IN(A) uint64_t A;std::cin>>A;
template<typename T> void printV(vector<T> V) {
for (int i=0;i<V.size()-1;i++) {
cout<<V[i]<<" ";
}
cout<<V[V.size()-1];
cout<<endl;
}
int64_t max3(int64_t a,int64_t b,int64_t c) {
return max(a,max(b,c));
}
struct compute {
int tests=1;
vector<int> data;
vector<pair<int,int>> result;
vector<bool> visited;
bool start() {
IN(a);
IN(M);
auto af=factors(a);
auto Mf=factors(M);
if (a==0) {
cout<<"-1"<<endl;
return false;
}
bool equal=false;
for (int i=0;i<af.size();i++) {
for (int j=0;j<Mf.size();j++) {
if (af[i]==Mf[j]) {
equal=true;
break;
};
}
}
if (equal) {
cout<<"-1"<<endl;
return false;
}
auto phi=totient(Mf);;
cout<<modPow(a,phi-1,M)<<endl;
return true;
}
void readV(int n) {
for (int i=0;i<n;i++) cin>>data[i];
}
uint64_t modPow(uint64_t a, uint64_t b, int m) {
if (b==0) return 1;
uint64_t u=modPow(a,b/2,m);
u=(u*u)%m;
if (b&1) u=(u*a)%m;
return u;
}
vector<uint64_t> factors(int n) {
vector<uint64_t> f;
for (int x =2;x*x<=n;x++) {
while (n%x ==0) {
f.push_back(x);
n/=x;
}
}
if (n>1) f.push_back(n);
return f;
}
uint64_t totient(vector<uint64_t> &v) {
if (v.size()==1) return v[0]-1;
uint64_t result=1;
for ( int i=1;i<v.size();i++){
uint64_t current=v[i];
while (i+1<v.size()&¤t==v[i+1]) {
result*=current;
i++;
}
result*=(current-1);
}
return result;
}
void testloop(){
while (tests-->0) {
start();
}
}
};
int main() {
ios_base::sync_with_stdio(false);
// cin.tie(NULL);
compute comp;
comp.start();
}
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: ACCEPTED
| input |
|---|
| 184120 505187 |
| correct output |
|---|
| 438779 |
| user output |
|---|
| 438779 |
Test 10
Verdict: ACCEPTED
| input |
|---|
| 264601 885661 |
| correct output |
|---|
| 360221 |
| user output |
|---|
| 360221 |
Test 11
Verdict: ACCEPTED
| input |
|---|
| 40310 590135 |
| correct output |
|---|
| -1 |
| user output |
|---|
| -1 |
Test 12
Verdict: ACCEPTED
| input |
|---|
| 202254499 577081420 |
| correct output |
|---|
| 128866679 |
| user output |
|---|
| 128866679 |
Test 13
Verdict: ACCEPTED
| input |
|---|
| 539836073 888851205 |
| correct output |
|---|
| 797044652 |
| user output |
|---|
| 797044652 |
Test 14
Verdict: ACCEPTED
| input |
|---|
| 697847215 756971670 |
| correct output |
|---|
| -1 |
| user output |
|---|
| -1 |
