-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path70 - Totient permutation.cpp
82 lines (74 loc) · 1.67 KB
/
70 - Totient permutation.cpp
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
#include <iostream>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <vector>
using namespace std;
#define pb push_back
#define mp make_pair
#define ll long long
#define ull unsigned ll
#define db double
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define PII pair<int, int>
const int MXN=10000000;
bool p[MXN+10];
vector<int> primes;
int phi[MXN+10];
int cnt1[10],cnt2[10];
bool foo(int x,int y) {
memset(cnt1,0,sizeof(cnt1));
memset(cnt2,0,sizeof(cnt2));
while (x) {
cnt1[x%10]++;
x/=10;
}
while (y) {
cnt2[y%10]++;
y/=10;
}
for (int i=0;i<10;i++) {
if (cnt1[i]!=cnt2[i]) return false;
}
return true;
}
int main() {
p[1]=1;
for (int i=2;i<=MXN;i++) {
if (!p[i]) {
primes.pb(i);
phi[i]=i-1;
}
for (int j=0;j<primes.size()&&i*primes[j]<=MXN;j++) {
p[i*primes[j]]=1;
if (i%primes[j]==0) {
int x=i;
int y=primes[j];
int tmp=y;
while (x%y==0) {
x/=y;
tmp*=y;
}
if (x==1) {
phi[tmp]=tmp*(y-1)/y;
} else {
phi[x*tmp]=phi[x]*phi[tmp];
}
break;
}
phi[i*primes[j]]=phi[i]*phi[primes[j]];
}
}
db ratio=2.0;
int cand=-1;
for (int i=2;i<=MXN;i++) {
if (foo(i,phi[i])) {
if ((db)i/(db)phi[i]<ratio) {
cand=i;
ratio=(db)i/(db)phi[i];
}
}
}
printf("phi(%d):%d\n", cand, phi[cand]);
}