更好的阅读体验

Portal

Portal1: Codeforces

Portal2: Luogu

Description

Recently a Golden Circle of Beetlovers was found in Byteland. It is a circle route going through \(n \cdot k\) cities. The cities are numerated from \(1\) to \(n \cdot k\), the distance between the neighboring cities is exactly \(1\) km.

Sergey does not like beetles, he loves burgers. Fortunately for him, there are \(n\) fast food restaurants on the circle, they are located in the \(1\)-st, the \((k + 1)\)-st, the \((2k + 1)\)-st, and so on, the \(((n-1)k + 1)\)-st cities, i.e. the distance between the neighboring cities with fast food restaurants is \(k\) km.

Sergey began his journey at some city \(s\) and traveled along the circle, making stops at cities each \(l\) km (\(l > 0\)), until he stopped in \(s\) once again. Sergey then forgot numbers \(s\) and \(l\), but he remembers that the distance from the city \(s\) to the nearest fast food restaurant was \(a\) km, and the distance from the city he stopped at after traveling the first \(l\) km from \(s\) to the nearest fast food restaurant was \(b\) km. Sergey always traveled in the same direction along the circle, but when he calculated distances to the restaurants, he considered both directions.

Now Sergey is interested in two integers. The first integer \(x\) is the minimum number of stops (excluding the first) Sergey could have done before returning to \(s\). The second integer \(y\) is the maximum number of stops (excluding the first) Sergey could have done before returning to \(s\).

Input

The first line contains two integers \(n\) and \(k\) (\(1 \le n, k \le 100\,000\)) — the number of fast food restaurants on the circle and the distance between the neighboring restaurants, respectively.

The second line contains two integers \(a\) and \(b\) (\(0 \le a, b \le \frac{k}{2}\)) — the distances to the nearest fast food restaurants from the initial city and from the city Sergey made the first stop at, respectively.

Output

Print the two integers \(x\) and \(y\).

Sample Input1

2 3
1 1

Sample Output1

1 6

Sample Input2

3 2
0 0

Sample Output2

1 3

Sample Input3

1 10
5 3

Sample Output3

5 5

Hint

In the first example the restaurants are located in the cities \(1\) and \(4\), the initial city \(s\) could be \(2\), \(3\), \(5\), or \(6\). The next city Sergey stopped at could also be at cities \(2, 3, 5, 6\). Let's loop through all possible combinations of these cities. If both \(s\) and the city of the first stop are at the city \(2\) (for example, \(l = 6\)), then Sergey is at \(s\) after the first stop already, so \(x = 1\). In other pairs Sergey needs \(1, 2, 3\), or \(6\) stops to return to \(s\), so \(y = 6\).

In the second example Sergey was at cities with fast food restaurant both initially and after the first stop, so \(l\) is \(2\), \(4\), or \(6\). Thus \(x = 1\), \(y = 3\).

In the third example there is only one restaurant, so the possible locations of \(s\) and the first stop are: \((6, 8)\) and \((6, 4)\). For the first option \(l = 2\), for the second \(l = 8\). In both cases Sergey needs \(x=y=5\) stops to go to \(s\).

Solution

我们根据题目的\(a, b, k\)计算出\(l\)的\(4\)种可能:

  1. \(a + b\)

  2. \(k - a + b\)

  3. \(k + a - b\)

  4. \(k - a - b\)

每走一步的答案就是\(\frac{n \times k}{\gcd(n \times k, step)}\)。

然后枚举找个最大的与最小的就可以了。

Code

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath> using namespace std; typedef long long LL;
const LL INF = 1e18;
LL n, k, a, b, step;
int main() {
scanf("%lld%lld%lld%lld", &n, &k, &a, &b);
LL Max = -INF, Min = INF;
step = fabs(a + b);//第1种情况
while (step <= n * k) {//枚举步数
if (step) {
Max = max(Max, n * k / __gcd(n * k, step));
Min = min(Min, n * k / __gcd(n * k, step));
}
step += k;
}
step = fabs(k - a + b);//第2种情况
while (step <= n * k) {//枚举步数
if (step) {
Max = max(Max, n * k / __gcd(n * k, step));
Min = min(Min, n * k / __gcd(n * k, step));
}
step += k;
}
step = fabs(k - b + a);//第3种情况
while (step <= n * k) {//枚举步数
if (step) {
Max = max(Max, n * k / __gcd(n * k, step));
Min = min(Min, n * k / __gcd(n * k, step));
}
step += k;
}
step = fabs(k - a - b);//第4种情况
while (step <= n * k) {//枚举步数
if (step) {
Max = max(Max, n * k / __gcd(n * k, step));
Min = min(Min, n * k / __gcd(n * k, step));
}
step += k;
}
printf("%lld %lld\n", Min, Max);
return 0;
}

『题解』Codeforces1142A The Beatles的更多相关文章

  1. 『题解』洛谷P1063 能量项链

    原文地址 Problem Portal Portal1:Luogu Portal2:LibreOJ Portal3:Vijos Description 在\(Mars\)星球上,每个\(Mars\)人 ...

  2. 『题解』Codeforces1142B Lynyrd Skynyrd

    更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description Recently Lynyrd and Skynyrd went to a ...

  3. 『题解』洛谷P1993 小K的农场

    更好的阅读体验 Portal Portal1: Luogu Description 小\(K\)在\(\mathrm MC\)里面建立很多很多的农场,总共\(n\)个,以至于他自己都忘记了每个农场中种 ...

  4. 『题解』洛谷P2296 寻找道路

    更好的阅读体验 Portal Portal1: Luogu Portal2: LibreOJ Description 在有向图\(\mathrm G\)中,每条边的长度均为\(1\),现给定起点和终点 ...

  5. 『题解』洛谷P1351 联合权值

    更好的阅读体验 Portal Portal1: Luogu Portal2: LibreOJ Description 无向连通图\(\mathrm G\)有\(n\)个点,\(n - 1\)条边.点从 ...

  6. 『题解』Codeforces656E Out of Controls

    更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description You are given a complete undirected gr ...

  7. 『题解』洛谷P2170 选学霸

    更好的阅读体验 Portal Portal1: Luogu Description 老师想从\(N\)名学生中选\(M\)人当学霸,但有\(K\)对人实力相当,如果实力相当的人中,一部分被选上,另一部 ...

  8. 『题解』洛谷P1083 借教室

    更好的阅读体验 Portal Portal1: Luogu Portal2: LibreOJ Portal3: Vijos Description 在大学期间,经常需要租借教室.大到院系举办活动,小到 ...

  9. 『题解』Codeforces9D How many trees?

    更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description In one very old text file there was wr ...

随机推荐

  1. 05-04 scikit-learn库之主成分分析

    目录 scikit-learn库之主成分分析 一.PCA 1.1 使用场景 1.2 代码 1.3 参数 1.4 属性 1.5 方法 二.KernelPCA 三.IncrementalPCA 四.Spa ...

  2. requests模块(代理)篇

    - 用户验证 - 代理验证 #可能需要使用HTTP basic Auth, 可以这样 # 格式为 用户名:密码@代理地址:端口地址 proxy = { "http": " ...

  3. ES三节点重启后报错no known master node

    问题 一直在研究ES的监控怎么做,想偷点懒,不去通过API获取然后计算,就想找个现成的插件或者监控软件,只要装个agent就可以,然后就找到了x-pack,插件装好了之后,需要重启ES集群,线上的ES ...

  4. Ubuntu和开发板用网线直连ping不通的问题

    我装的Ubuntu 18.04双系统,在通过网络加载内核和文件系统那一步一直连接不上,uboot里面ping我的主机IP地址,提示: ping failed; host 192.168.1.111 i ...

  5. Kali桥接模式DHCP自动获取IP失败(VMware)

    Kali桥接模式DHCP自动获取IP失败笔者用的是VMware运行Kali Linux,突然发现桥接模式无法上网,只能使用NAT模式.身为有一点点强迫症的人来说,这就很不爽了.于是马上切换为桥接模式, ...

  6. 数据结构2_java---栈,括号匹配

    package Main; import java.util.Scanner; import javax.swing.text.html.HTMLDocument.HTMLReader.Isindex ...

  7. 测试中常用sql

    1.增删改查 2.同一服务器下,要从一个数据库复制某张表到另一个数据库 create table test.sf_audit_plan as select * from v3_0_sf_full.sf ...

  8. Java中常用的四种线程池

    在Java中使用线程池,可以用ThreadPoolExecutor的构造函数直接创建出线程池实例,如何使用参见之前的文章Java线程池构造参数详解.不过,在Executors类中,为我们提供了常用线程 ...

  9. 在已有 Windows10 系统的基础上,安装 Ubuntu17.10 系统(新版 BIOS)

    1.第一步,下载Ubuntu系统镜像 2.第二步,制作启动U盘,使用UltralSO,步骤:打开文件——选择iso文件——启动——写入硬盘映像——选择U盘——写入 3.第三步,分区,在Windows徽 ...

  10. Solr分片机制以及Solrcloud搭建及分片操作

    Solr分片描述 分片是集合的逻辑分区,包含集合中文档的子集,这样集合中的每个文档都正好包含在一个分片中.集合中包含每个文档的分片取决于集合的整体"分片"策略. 当您的集合对于一个 ...