-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountSolutions.java
More file actions
100 lines (83 loc) · 2.29 KB
/
CountSolutions.java
File metadata and controls
100 lines (83 loc) · 2.29 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
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
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
/**
* No. of solutions to (x*x + y*y)== (x*a+y*b)
**/
public class CountSolutions {
static long countSolutions(int a, int b, int c, int d)
{
// Complete this function
long count=0;
long D;
double x1,x2;
if(d<c)
{
for(int y=1;y<=d;y++)
{
D=(a*a)-(4*(y*y-b*y));
if(D>0&& Math.sqrt(D)%1==0)
{
D=(int) Math.sqrt(D);
x1=(a+D)/2.0;
x2=(a-D)/2.0;
if(x1<=c &&x1>=1 && x1%1==0)
count++;
if(x2<=c && x2>=1 && x2%1==0)
count++;
}
else if(D==0)
{
x1=a/2;
if(x1<=c && x1>=1 && x1%1 == 0)
count++;
}
}
}
else
{
for(int x=1;x<=c;x++)
{
D=(b*b)-(4*(x*x-a*x));
if(D>0&& Math.sqrt(D)%1==0)
{
D=(int) Math.sqrt(D);
x1=(b+D)/2.0;
x2=(b-D)/2.0;
if(x1<=d &&x1>=1 && x1%1==0)
count++;
if(x2<=d && x2>=1 && x2%1==0)
count++;
}
else if(D==0)
{
x1=b/2;
if(x1<=d && x1>=1 && x1%1 == 0)
count++;
}
}
}
return count;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int q = in.nextInt();
if(q>=1&&q<=10)
{
for(int a0 = 0; a0 < q; a0++)
{
int a = in.nextInt();
int b = in.nextInt();
int c = in.nextInt();
int d = in.nextInt();
if(a>=1&&a<=100000 && b>=1&&b<=100000 && c>=1&&c<=100000 &&d>=1&&d<=100000)
{
long result = countSolutions(a, b, c, d);
System.out.println(result);
}
}
}
}
}