forked from xtaci/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGhosts.java
More file actions
50 lines (45 loc) · 1.51 KB
/
Copy pathGhosts.java
File metadata and controls
50 lines (45 loc) · 1.51 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
// https://www.hackerrank.com/contests/w18/challenges/ghosts/submissions/code/4284508
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static int gcd(int a, int b) {
if (b>0) {
return gcd(b, a%b);
}
return a;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int A = in.nextInt();
int B = in.nextInt();
int C = in.nextInt();
int D = in.nextInt();
int result = 0;
/*
The difference between the town number and the street number is divisible by 3.
The sum of the street number and the house number is divisible by 5.
The product of the town number and the house number is divisible by 4.
The greatest common divisor of the town number and the apartment number is 1.
*/
for (int a = 1; a <=A; a++) {
for (int b = 1; b <=B; b++) {
int diff = a - b;
if (diff % 3 != 0) continue;
for (int c = 1; c <=C; c++) {
int sum = b + c;
int product = a*c;
if (sum % 5 != 0 || product %4 !=0) continue;
for (int d = 1; d <=D; d++) {
int gcd = gcd(a, d);
if (gcd != 1) continue;
result+=1;
}
}
}
}
System.out.println(result);
}
}