E1 - Divisible Numbers (easy version)(hard version)

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 / (a * b / gcd(a * b, x)),判断是否存在一个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个

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值