原题链接:
题目描述:
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,b,c,d,问有没有x,y满足a < x <= c, b < y <= d且x * y能被a * b整除,如果有多组答案,则输出任意一组x,y即可,如果不存在答案,则输出-1。
解题思路:
数据不大,1e5,O(n)是完全可承受的,我们可以枚举x,设e = a * b / gcd(a * b, x),则任何一个大于b且小于等于d的e的倍数都可以满足题目的需求。我们让y = (b + e) / e * e即可得到最小的大于b的e的倍数,只要他同时满足小于等于d,那么这个x和y就可以是答案。如果枚举完所有的x都找不到合适的y,即无解。
代码(CPP):
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
typedef unsigned long long ull;
const int maxn = 1e3 + 10;
const int INF = 0x3fffffff;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout << fixed;
cout.precision(18);
int t;
cin >> t;
while(t--)
{
int a, b, c, d;
cin >> a >> b >> c >> d;
bool flag = false;
for (int x = a + 1; x <= c; x++)
{
int e = a * b / __gcd(a * b , x);
int y = (e + b) / e * e;
if(y > b && y <= d)
{
cout << x << " " << y << endl;
flag = true;
break;
}
}
if(!flag)
cout << "-1 -1\n";
}
return 0;
}