This is an easy version of the problem. The only difference between an easy and a hard version is the constraints on aa, bb, cc and dd.
You are given 44 positive integers aa, bb, cc, dd with a<ca<c and b<db<d. Find any pair of numbers xx and yy that satisfies the following conditions:
- a<x≤ca<x≤c, b<y≤db<y≤d,
- x⋅yx⋅y is divisible by a⋅ba⋅b.
Note that required xx and yy may not exist.
Input
The first line of the input contains a single integer tt (1≤t≤10(1≤t≤10), the number of test cases.
The descriptions of the test cases follow.
The only line of each test case contains four integers aa, bb, cc and dd (1≤a<c≤1051≤a<c≤105, 1≤b<d≤1051≤b<d≤105).
Output
For each test case print a pair of numbers a<x≤ca<x≤c and b<y≤db<y≤d such that x⋅yx⋅y is divisible by a⋅ba⋅b. If there are multiple answers, print any of them. If there is no such pair of numbers, then print -1 -1.
简单版本:原问题为在a + 1 ---- c中找一个x, 在 b + 1 --- d中找一个y, 使得x * y 是 a * b 的倍数
将原问题转化为,判断是否存在一个y,是底下数的倍数,只需要判断最大的 d 是不是即可,因为如果最大的都不满足一定不存在,这样我们只需要枚举x,然后用O(1)的时间找出答案
#include<iostream>
#define int long long
using namespace std;
int gcd(int a, int b)
{
return b ? gcd(b, a % b) : a;
}
signed main()
{
int t;
cin >> t;
while(t --)
{
bool flag = false;
int a, b, c, d;
cin >> a >> b >> c >> d;
for(int x = a + 1; x <= c; x ++)
{
int s = a * b / (gcd(a * b, x));
if(d / s * s >= b + 1)
{
flag = true;
cout << x << " " << d / s * s << endl;
break;
}
}
if(!flag)
cout << "-1 -1" << endl;
}
}
然后继续优化,之前我们枚举x, 现在x是1e9,所以不能直接枚举。我们实际需要的是gcd( a * b, x ),我们只需要枚举a * b的约数即可,a * b的约数是a 的约数和 b 的约数乘法原理,一个不超过1e9的数的约数个数最多是1344个,所以总共有1344 * 1344个