-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsol.cpp
More file actions
executable file
·85 lines (81 loc) · 1.91 KB
/
Copy pathsol.cpp
File metadata and controls
executable file
·85 lines (81 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <bits/stdc++.h>
#define EPS 1e-9
using namespace std;
/*
* calculates (a * b) % c taking into account that a * b might overflow
*/
long long int mulmod(long long int a, long long int b,
long long int mod) {
long long int x = 0,y = a % mod;
while (b > 0)
{
if (b % 2 == 1)
{
x = (x + y) % mod;
}
y = (y * 2) % mod;
b /= 2;
}
return x % mod;
}
/*
* modular exponentiation
*/
long long int modulo(long long int base, long long int exponent,
long long int mod) {
long long int x = 1;
long long int y = base % mod;
while (exponent > 0) {
if (exponent % 2 == 1) {
x = (x * y) % mod;
}
y = (y * y) % mod;
exponent = exponent / 2;
}
return x % mod;
}
/*
* Miller-Rabin primality test, iteration signifies the accuracy
*/
bool Miller(long long int p,int iteration) {
if (p < 2 || (p != 2 && p % 2==0)) {
return false;
}
long long int s = p - 1;
while (s % 2 == 0) {
s /= 2;
}
for (int i = 0; i < iteration; i++) {
long long int a = rand() % (p - 1) + 1;
long long int temp = s;
long long int mod = modulo(a, temp, p);
while (temp != p - 1 && mod != 1 && mod != p - 1) {
mod = mulmod(mod, mod, p);
temp *= 2;
}
if (mod != p - 1 && temp % 2 == 0) {
return false;
}
}
return true;
}
int main() {
int q;
cin >> q;
int k = 1;
double root;
double roundroot;
do {
root = pow(q, 1.0/ (double) k);
roundroot = round(root);
if (abs(roundroot - root) < EPS) { // if round digit
if (Miller((long long) roundroot, 16)) {
cout << "yes" << endl;
return 0;
}
}
k++;
} while (root >= 2.0);
cout << "no" << endl;
return 0;
}