E1. Divisible Numbers (easy version)

E1. Divisible Numbers (easy version)

This is an easy version of the problem. The only difference between an easy and a hard version is the constraints on a,b,c and d.

You are given 4 positive integers a,b,c,d with a<c and b<d. Find any pair of numbers x and y that satisfies the following conditions:

  • a<xc , b<yd,
  • xy is divisible by ab.

Note that required x and y may not exist.

Input

The first line of the input contains a single integer t (1t10), the number of test cases.

The descriptions of the test cases follow.

The only line of each test case contains four integers a,b,c and d (1a<c105,1b<d105).

Output

For each test case print a pair of numbers a<xc and b<yd such that xy is divisible by ab. If there are multiple answers, print any of them. If there is no such pair of numbers, then print -1 -1.

Example

input

5
1 1 2 2
3 4 5 7
8 9 15 18
12 21 14 24
36 60 48 66

output

2 2
4 6
12 12
-1 -1
-1 -1

 

解题思路

  容易想到的暴力做法是双重循环枚举xy,这样做肯定会超时,因此可以想一下能否只枚举xy则根据abxy这个条件直接求出来。

  因为xyab整除,因此y必然被abgcd(ab,x)整除。令t=abgcd(ab,x),因此当我们枚举到某个x,看一下在区间[b+1,d]内是否存在一个t的倍数,设这个数为y,如果存在则表示x,y就是一组解。

  如何判断在[b+1,d]内是否存在一个t的倍数呢?只要看一下严格大于b的最小的t的倍数是否不超过d就可以了。

  • 如果tb,那么很明显严格大于b的最小的t的倍数是(bt+1)×t
  • 如果tb,那么严格大于b的最小的t的倍数是bt×t

  因此可以统一表示成(bt+1)×t

  AC代码如下:

复制代码
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 typedef long long LL;
 5 
 6 int gcd(LL a, LL b) {
 7     return b ? gcd(b, a % b) : a;
 8 }
 9 
10 void solve() {
11     int a, b, c, d;
12     cin >> a >> b >> c >> d;
13     LL k = 1ll * a * b;
14     for (int i = a + 1; i <= c; i++) {    // 枚举x 
15         LL t = k / gcd(k, i);    // t|y,t是y的因子 
16         if ((b / t + 1) * t <= d) {    // 大于b的最小的t的倍数不超过d 
17             printf("%d %d\n", i, d / t * t);
18             return;
19         }
20     }
21     printf("-1 -1\n");
22 }
23 
24 int main() {
25     int t;
26     scanf("%d", &t);
27     while (t--) {
28         solve();
29     }
30     
31     return 0;
32 }
复制代码

 

参考资料

  Codeforces Round #828 (Div. 3) Editorial:https://codeforces.com/blog/entry/108101

posted @   onlyblues  阅读(282)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 凌晨三点救火实录:Java内存泄漏的七个神坑,你至少踩过三个!
Web Analytics
点击右上角即可分享
微信分享提示