做法是对 a,b 分解因数,然后计算出 ab 的因数。
然后遍历 ab 的因数作为 x,O(1) 计算出对应的 y。
复杂度是 ab 的因数数量,1e6 级别,再乘上 T 就是 1e7。按理说能过。
然后 TLE on test25 了。
import sys
import math
input = sys.stdin.readline
def great_sqrt(x):
_ = int(math.sqrt(x))
while _*_ < x:
_+=1
while _*_ > x:
_-=1
return _
if __name__ == "__main__":
t = int(input())
while t!=0:
a,b,c,d=map(int,input().split())
_divs=[]
_divs2=[]
for i in range(1,great_sqrt(a)+1):
if a%i==0:
_divs.append(i)
if a/i != i:
_divs.append(a/i)
for i in range(1,great_sqrt(b)+1):
if b%i==0:
_divs2.append(i)
if b/i != i:
_divs2.append(b/i)
divs = []
for div in _divs:
for div2 in _divs2:
divs.append(div*div2)
# print("(%d,%d)"%(div,div2))
divs.append(c)
print(len(divs))
ok=0
for x in divs:
x_tmp = x
if (a-1)//x_tmp != c//x_tmp:
x = ((a-1)//x_tmp)*x_tmp + x_tmp
# print("tempy = %d"%(y))
if x+x_tmp <= c:
x=x+x_tmp
if x <= a or x > c:
continue
if x<=a or x > c:
continue
# print("x = %d"%(x))
# print("y_tmp = %d"%(y_tmp))
if x==c:
y_tmp=a*b
else:
y_tmp = a*b/int(x)
if (b-1)//y_tmp != d//y_tmp:
y = ((b-1)//y_tmp)*y_tmp + y_tmp
# print("tempy = %d"%(y))
if y+y_tmp <= d:
y=y+y_tmp
if y <= b or y > d:
continue
print("%d %d"%(x,y),end='\n')
ok=1
break
if ok==0:
print("-1 -1",end='\n')
# print("=====================")
t-=1