题目 | Divisible Numbers
Codeforces Round #828 (Div. 3)
E1. Divisible Numbers (easy version)
https://codeforces.com/contest/1744/problem/E1
E2. Divisible Numbers (hard version)
https://codeforces.com/contest/1744/problem/E2
4 seconds / 256 megabytes
standard input / standard output
Problem
You are given
, , is divisible by .
Note that required
Input
The first line of the input contains a single integer
The descriptions of the test cases follow.
E1: easy version
The only line of each test case contains four integers
E2: hard version
The only line of each test case contains four integers
Output
For each test case print a pair of numbers
Example
E1: easy version
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
E2: hard version
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
E1 题解
简单版的数据规模为
算法思路
对于一组
之所以要找最小的
如果对于任意
最小的 的求法
由
变形得:
又因为
综上:
代码
- 时间复杂度:
- 空间复杂度:
void solve()
{
ll a, b, c, d;
cin >> a >> b >> c >> d;
for (ll x = a + 1; x <= c; x++)
{
ll y = a * b / __gcd(a * b, x); // 计算最小的y
y *= b / y + 1; // 将y扩大
if (y <= d)
{
cout << x << ' ' << y << endl;
return;
}
}
cout << -1 << ' ' << -1 << endl;
return;
}
E2 题解
困难版的数据规模为
算法思路
我们要求满足
若存在一组满足条件的
拆分方式
我们将
则我们可以令:
代码
- 时间复杂度:
- 空间复杂度:
void solve()
{
ll a, b, c, d;
cin >> a >> b >> c >> d;
vector<ll> fact_a, fact_b;
for (ll i = 1; i * i <= a; i++)
{
if (!(a % i))
{
fact_a.push_back(i);
if (a / i != i)
fact_a.push_back(a / i);
}
}
for (ll i = 1; i * i <= b; i++)
{
if (!(b % i))
{
fact_b.push_back(i);
if (b / i != i)
fact_b.push_back(b / i);
}
}
for (auto i : fact_a)
{
for (auto j : fact_b)
{
ll p = i * j, q = a * b / (i * j);
ll x = (a / p + 1) * p, y = (b / q + 1) * q;
if (x <= c && y <= d)
{
cout << x << ' ' << y << endl;
return;
}
x = (a / q + 1) * q, y = (b / p + 1) * p;
if (x <= c && y <= d)
{
cout << x << ' ' << y << endl;
return;
}
}
}
cout << -1 << ' ' << -1 << endl;
return;
}
本文采用 CC BY-SA 4.0 许可,本文 Markdown 源码:Haotian-BiJi