更好的阅读体验

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. Ned 的难题

    题目描述 Ned 再也看不下去 Robert 的种种恶习, 于是他决定出一道题来让他醒悟. Ned 的题目是这样: 给出一个有 n 个数的序列, 求其中所有连续子序列的数的最大公因数的乘积模 1000 ...

  2. 利用sed将xml报文转换为分隔符形式报文

    原始xml文本如下 <?xml version="1.0" encoding="utf-8"?> <Message> <Heade ...

  3. Linux 命令之 mkdir

    mkdir的作用是创建一个目录,可以理解为 make directory 的缩写. 创建目录 mkdir dir_name 在当前目录创建一个名为 dir_name 的目录. 同时创建多级目录 假设现 ...

  4. Faith 信念

    Today I’d like to talk about faith. With faith, you’ll go further and never be lost. Faith is free a ...

  5. Map集合(双列集合)

    Map集合(双列集合)Map集合是键值对集合. 它的元素是由两个值组成的,元素的格式是:key=value. Map集合形式:{key1=value1 , key2=value2 , key3=val ...

  6. Java零基础手把手系列:HashMap排序方法一网打尽

    HashMap的排序在一开始学习Java的时候,比较容易晕,今天总结了一些常见的方法,一网打尽.HashMap的排序入门,看这篇文章就够了. 1. 概述 本文排序HashMap的键(key)和值(va ...

  7. openflow流表项中有关ip掩码的匹配的问题(控制器为ryu)

    一.写在前面 唉,被分配到sdn安全方向,顶不住,顶不住,感觉搞不出来什么有搞头的东西.可若是让我水水的应付,我想我也是做不到的,世上无难事只怕有心人.好了,进入正题,本次要讨论的时一个比较细节的东西 ...

  8. 《HTML5+CSS3+JavaScript 从入门到精通(标准版)》学习笔记(二)

    这是一个应用的例子,学以致用嘛 <!--这些代码我就直接放在了博客园的"页首Html代码"中,用于自定义博客,效果就是页面左上角的白色文字--> <p> & ...

  9. 百万年薪python之路 -- 装饰器进阶

    本文链接:https://blog.csdn.net/xiemanR/article/details/72510885 一:函数装饰函数 def wrapFun(func): def inner(a, ...

  10. JAVA实现扫描线算法

    首先说一下,教科书上的扫描线算法确实是用c++很好实现,而且网上有很多源码,而java实现的基本没有(可能是我没看到),所以肖先生还是打算自己码(实验作业写这个而自己又个是写java的猿0.0). 对 ...