-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKiritoInLabyrinth.java
More file actions
72 lines (58 loc) · 1.88 KB
/
KiritoInLabyrinth.java
File metadata and controls
72 lines (58 loc) · 1.88 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
package problems.codechef;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
/**
* Created by hardCode on 12/2/2016.
*/
public class KiritoInLabyrinth {
static final int MAX=100001;
static int a[]=new int[MAX];
static int dp[]=new int[MAX];
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t,n;
String s[];
t=Integer.parseInt(br.readLine());
while (t-->0){
n=Integer.parseInt(br.readLine());
Arrays.fill(dp,0,n,1);//initialize the array
s=br.readLine().split("\\s");
for (int i = 0; i < n; i++) {
a[i]=Integer.parseInt(s[i]);
}
System.out.println(solve(n));
}
}
private static int solve(int n) {
return lisGCD(n);
}
//This is the function calculates the longest increasing subsequence with gcd
private static int lisGCD(int n) {
int res=1;
for (int i = 1; i <n ; i++) {
for (int j = i-1; j>=0 ; j--) {
if (gcd(a[i],a[j])>1){
dp[i]=Integer.max(dp[i],dp[j]+1);
}
}
res=Integer.max(res,dp[i]);
}
return res;
}
static int gcd(int p, int q) {
if (q == 0) return p;
if (p == 0) return q;
// p and q even
if ((p & 1) == 0 && (q & 1) == 0) return gcd(p >> 1, q >> 1) << 1;
// p is even, q is odd
else if ((p & 1) == 0) return gcd(p >> 1, q);
// p is odd, q is even
else if ((q & 1) == 0) return gcd(p, q >> 1);
// p and q odd, p >= q
else if (p >= q) return gcd((p-q) >> 1, q);
// p and q odd, p < q
else return gcd(p, (q-p) >> 1);
}
}