E1:
E1. Divisible Numbers (easy version)
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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<x≤c, b<y≤d
- x⋅yis divisible by a⋅b.
Note that required x and y may not exist.
Input
The first line of the input contains a single integer t (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≤10^5, 1≤b<d≤10^5).
Output
For each test case print a pair of numbers a<x≤c and b<y≤d such that x⋅y is divisible by a⋅b. 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
#include<bits/stdc++.h>
using namespace std;
#define int long long
int a,b,c,d,s;
int ck(int lx,int rx,int now)
{
int l=(lx/now)+1,r=rx/now;
if(l<=r) return l*now;
return -1;
}
void solve()
{
cin>>a>>b>>c>>d;
s=a*b;
for (int x=1; x*x<=s; x++)
{
if (s%x==0)
{
int x2=ck(a,c,x);
int y2=ck(b,d,s/x);
if (x2==-1||y2==-1)
{
x2=ck(a,c,s/x);
y2=ck(b,d,x);
if (x2==-1||y2==-1) continue;
else
{
cout<<x2<<" "<<y2<<"\n";
return;
}
}
else
{
cout<<x2<<" "<<y2<<"\n";
return;
}
}
}
cout<<"-1 -1\n";
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
cin>>T;
while(T--)
{
solve();
}
return 0;
}
E2. Divisible Numbers (hard version)
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
This is an hard 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<x≤c, b<y≤d,
- x⋅y is divisible by a⋅b.
Note that required x and y may not exist.
Input
The first line of the input contains a single integer t (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≤10^9, 1≤b<d≤10^9).
Output
For each test case print a pair of numbers a<x≤c and b<y≤d such that x⋅y is divisible by a⋅b. If there are multiple answers, print any of them. If there is no such pair of numbers, then print -1 -1.
Example
input
10
1 1 2 2
3 4 5 7
8 9 15 18
12 21 14 24
36 60 48 66
1024 729 373248 730
1024 729 373247 730
5040 40320 40319 1000000000
999999999 999999999 1000000000 1000000000
268435456 268435456 1000000000 1000000000
output
2 2 4 6 12 12 -1 -1 -1 -1 373248 730 -1 -1 15120 53760 -1 -1 536870912 536870912
#include<bits/stdc++.h>
using namespace std;
#define int long long
vector<int>v1,v2;
void solve()
{
int a,b,c,d;
cin>>a>>b>>c>>d;
v2.clear(),v1.clear();
for(int i=1; i*i<=a; i++)
if(a%i==0)
v1.push_back(i),v1.push_back(a/i);
for(int i=1; i*i<=b; i++)
if(b%i==0)
v2.push_back(i),v2.push_back(b/i);
for(int i=0; i<v1.size(); i++)
for(int j=0; j<v2.size(); j++)
{
int u=v1[i]*v2[j];
int v=a*b/u;
int x=(a/u+1)*u,y=(b/v+1)*v;
if(x<=c&&y<=d)
{
cout<<x<<" "<<y<<"\n";
return;
}
}
cout<<"-1 -1"<<"\n";
return;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
cin>>T;
while(T--)
{
solve();
}
return 0;
}