更好的阅读体验

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. 游戏服务器和Web服务器的区别

    用Go语言写游戏服务器也有一个多月了,也能够明显的感受到两者的区别.这篇文章就是想具体的聊聊其中的区别.当然,在了解区别之间,我们先简单的了解一下Go语言本身. 1. Go语言的特点 Go语言跟其他的 ...

  2. redis等缓存

    文章出处 https://www.cnblogs.com/wupeiqi/articles/5246483.html Model 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: ...

  3. 构建gitlab+Jenkins+harbor+kubernetes的DevOps持续集成持续部署环境

    构建gitlab+Jenkins+harbor+kubernetes的DevOps持续集成持续部署环境 整个环境的结构图. 一.准备工作 gitlab和harbor我是安装在kubernetes集群外 ...

  4. 2019头条java面试总结 (包含面试题解析)

    2019滴滴java面试总结  (包含面试题) 本人8年开发经验.今年年初找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.滴滴等公司offer,岗位是Java后端开发. 面试了很多家公司,感觉大部分 ...

  5. unittest介绍

    unittest框架是python中一个标准的库中的一个模块,该模块包括许多的类如 test case类.test suit类.texttest runner类.texttest resuite类.t ...

  6. python selenium鼠标滑动操作

    先安装pyautogui: pip install pyautogui #coding=utf-8 import pyautogui from selenium import webdriver fr ...

  7. Windows 服务程序(一)

    Windows 服务程序简介: Windows服务应用程序是一种需要长期运行的应用程序,它对于服务器环境特别适合. 它没有用户界面,并且也不会产生任何可视输出.任何用户消息都会被写进Windows事件 ...

  8. Web安全之变量覆盖漏洞

    通常将可以用自定义的参数值替换原有变量值的情况称为变量覆盖漏洞.经常导致变量覆盖漏洞场景有:$$使用不当,extract()函数使用不当,parse_str()函数使用不当,import_reques ...

  9. 代码审计-MetInfo CMS变量覆盖漏洞

    0x01 代码分析 安装好后是这样的 漏洞文件地址\include\common.inc.php 首先是在这个文件发现存在变量覆盖的漏洞 foreach(array('_COOKIE', '_POST ...

  10. 实验吧之【Forms、天网管理系统】

    Forms 原题链接 http://ctf5.shiyanbar.com/10/main.php Form 其实是个提示,代表html表单 F12 查看源码,发现 <input name=&qu ...