-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcd ordering.txt
79 lines (78 loc) · 2.12 KB
/
gcd ordering.txt
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
int gcd(int a,int b)
{
if(a>b)
return gcd(b,a);
else if(a==0 && a==b)
return 0;
else if(a==0)
return b;
else
return __gcd(a,b);
}
vector<int> solvee(vector<int> &a){
int n;
n=a.size();
vector<int> ans;
assert(n>=1&&n<=100000);
int i,flag,ps;
sort(a.begin(),a.end());
if (n<3){
for (i=n-1;i>=0;i--)
ans.push_back(a[i]);
return ans;
}
else{
flag=0;
for (i=2;i<n;i++){
if(a[i]!=a[i-1]+gcd(a[i-1],a[i-2])){
flag=1;
ps=i;
break;
}
}
if (flag==0){
if (a[1]==a[0]+gcd(a[0],a[n-1])){
ans.push_back(a[n-1]);
for(i=0;i<n-1;i++)
ans.push_back(a[i]);
return ans;
}
else{
for(i=0;i<n;i++)
ans.push_back(a[i]);
return ans;
}
}
else{
if (a[1]==a[0]+gcd(a[ps],a[0])){
flag=0;
for(i=n-1;i>ps+2;i--){
if(a[i]!=a[i-1]+gcd(a[i-1],a[i-2])){
flag=1;
break;
}
}
if (flag==0 and ps<n-1){
if (a[ps+1]!=a[ps-1]+gcd(a[ps-1],a[ps-2]))
flag=1;
}
if (flag==0 and ps<n-2){
if (a[ps+2]!=a[ps+1]+gcd(a[ps-1],a[ps+1]))
flag=1;
}
if (flag==0){
ans.push_back(a[ps]);
for(i=0;i<n;i++)
if(i!=ps)
ans.push_back(a[i]);
return ans;
}
}
}
}
ans.push_back(-1);
return ans;
}
vector<int> Solution::solve(vector<int> &A) {
return solvee(A);
}