-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProblem_#95_Amicable_chains.cpp
108 lines (101 loc) · 1.83 KB
/
Problem_#95_Amicable_chains.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <cstdio>
#include <iostream>
#include <sstream>
#include <string>
#include <cmath>
#include <cassert>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <deque>
using namespace std;
typedef long long ll;
typedef pair<double, double> dd;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
int main(){
int k; cin >> k;
int n = k+3;
vector<map<int, int> > v(n);
vi p(n, 0);
for(int i=2;i<n;i++){
if(!p[i]){
for(int j=i;j<n;j+=i){
p[j] = 1;
v[j][i] = 1;
}
}
}
vector<ll> s(n);
s[0] = s[1] = 1<<30;
for(int i=2;i<=k;i++){
ll sum = 1;
int x = i;
for(map<int,int>::iterator it=v[i].begin();it!=v[i].end();it++){
int a = it->first;
ll r = a;
while(x%a == 0){
x /= a;
r *= a;
}
r -= 1;
r /= (a-1);
sum *= r;
}
sum -= i;
s[i] = sum;
//cout << i <<" "<< sum <<endl;
}
vi vis(n, 0);
vi stack;
vi res(n, 0);
vi dis(n, 0);
int counter = 0;
int mx = -1;
int mxi = -1;
for(int i=6;i<=k;i++){
if(!vis[i]){
int at = i;
while(true){
stack.push_back(at);
vis[at] = 1;
dis[at] = ++counter;
int to = s[at];
if(to > k || to < 6) break;
if(vis[to] == 0){
at = to;
continue;
}else if(vis[to] == 1){
int len = dis[at]-dis[to]+1;
while(stack.back() != to){
int z = stack.back(); stack.pop_back();
vis[z] = 2;
res[z] = len;
}
int z = stack.back(); stack.pop_back();
vis[z] = 2;
res[z] = len;
break;
}else{
break;
}
}
while(!stack.empty()){
int z = stack.back(); stack.pop_back();
vis[z] = 2;
}
}
//if(res[i] > 0) cout << i << " "<<res[i] <<endl;
if(res[i] > mx){
mx = res[i];
mxi = i;
}
}
//cout << mx <<endl;
cout << mxi << endl;
}