-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGridL.java
More file actions
38 lines (37 loc) · 1.1 KB
/
Copy pathGridL.java
File metadata and controls
38 lines (37 loc) · 1.1 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
import java.util.*;
public class GridL{
public static void main(String[]args){
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
StringBuilder sb=new StringBuilder();
int tt=Integer.parseInt(s);
while(tt-->0){
long p=sc.nextLong();
long q=sc.nextLong();
long t=2*p+4*q+1;
long sqrtt=(long)Math.sqrt((double)t);
while((sqrtt+1)*(sqrtt+1)<=t)sqrtt++;
while(sqrtt*sqrtt>t)sqrtt--;
long bestA=-1;
for(long a=sqrtt;a>=3;a--){
if(t%a==0){
bestA=a;
break;
}
}
if(bestA==-1)
sb.append("-1\n");
else{
long b=t/bestA;
if(b-bestA<=2*p){
long n=(bestA-1)/2;
long m=(b-1)/2;
sb.append(n).append(' ').append(m).append("\n");
}else{
sb.append("-1\n");
}
}
}
System.out.print(sb);
}
}