https://codeforces.com/contest/1143/problem/D

D. The Beatles
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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

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

Sergey began his journey at some city ss and traveled along the circle, making stops at cities each ll km (l>0l>0), until he stopped in ss once again. Sergey then forgot numbers ss and ll, but he remembers that the distance from the city ss to the nearest fast food restaurant was aakm, and the distance from the city he stopped at after traveling the first ll km from ss to the nearest fast food restaurant was bb 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 xx is the minimum number of stops (excluding the first) Sergey could have done before returning to ss. The second integer yy is the maximum number of stops (excluding the first) Sergey could have done before returning to ss.

Input

The first line contains two integers nn and kk (1≤n,k≤1000001≤n,k≤100000) — the number of fast food restaurants on the circle and the distance between the neighboring restaurants, respectively.

The second line contains two integers aa and bb (0≤a,b≤k20≤a,b≤k2) — 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 xx and yy.

Examples
input
2 3
1 1
output
1 6
input
3 2
0 0
output
1 3
input
1 10
5 3
output
5 5
Note

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

In the second example Sergey was at cities with fast food restaurant both initially and after the first stop, so ll is 22, 44, or 66. Thus x=1x=1, y=3y=3.

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

题意:有一个总长度为n*K的环,每隔K有一个饭店,你的初始位置不知道,只知道你初始位置距离最近的饭店距离为 A,你每次移动L米然后停下来,但是L也未知,但是知道你第一次停下来距离你最近的饭店距离为B,你再次停在初始位置时结束旅行,求你可能的最大停留次数和最小停留次数

题解:由题意可得几个等式:等式一:ans*L=n*K*t,并且gcd(ans,t)==1,也就是你经过ans次之后停在了初始位置 等式二:L=(a+b)+t1*K  等式三:L=(-a-b)+t2*K 等式四:L=(a-b)+t3*K 等式五:L=(b-a)+t4*K 等式二~五是初始位置和第一次停留的可能情况,而等式一变形为L=n*K/ans * t,则可以分别和等式二~五联立,得到诸如 n*K/ans *t - K*t1 = (a+b)的式子,而ans必须有 n*k%ans==0,所以ans的个数就是n*k的因子数,n*k<=1e10,所以ans的个数很少,所以可以直接通过枚举n*K的因子ans再利用扩展欧几里德来求解t,并且判断 gcd(ans,t)是否=1即可

 #include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
ll gcd(ll x,ll y){
if(y==)return x;
return gcd(y,x%y);
}
ll qwq[],tot,qaq[],tot2; ll exgcd(ll m, ll n, ll &x, ll &y) {
if (n == ) {
x = ; y = ;
return m;
}
ll a, a1, b, b1, c, d, q, r, t;
a1 = b = ;
a = b1 = ;
c = m; d = n; q = c/d; r = c%d;
while (r) {
c = d;
d = r;
t = a1;
a1 = a;
a = t - q * a;
t = b1;
b1 = b;
b = t - q * b;
q = c/d;
r = c%d;
}
x = a; y = b;
return d;
}
int main(){
ll n,k,a,b;
scanf("%lld%lld%lld%lld",&n,&k,&a,&b);
ll ss=n*k;
for(ll i=;i*i<=ss;i++){
if(ss%i==){
qwq[++tot]=i;
if(ss!=i*i)qwq[++tot]=ss/i;
}
}
sort(qwq+,qwq++tot);
for(int i=;i<=tot;i++){
if((a+b)%gcd(n*k/qwq[i],k)==||(abs(a-b))%gcd(n*k/qwq[i],k)==){
printf("%lld ",qwq[i]);
break;
}
}
for(int i=tot;i>=;i--){
ll q1=gcd(n*k/qwq[i],k);
ll q2=gcd(n*k/qwq[i],k);
if(((a+b)%q1==)||(abs(a-b))%q2==){
if(((a+b)%q1==)){
ll xx,yy;
exgcd(n*k/qwq[i],k,xx,yy);
xx*=(a+b)/q1;
while(xx>&&k/q1)xx-=k/q1;
while(xx<=&&k/q1)xx+=k/q1;
if(gcd(xx,qwq[i])==){printf("%lld\n",qwq[i]);return ;}
}
if((abs(a-b))%q2==){
ll xx,yy;
exgcd(n*k/qwq[i],k,xx,yy);
xx*=abs(a-b)/q2;
while(xx>&&k/q2)xx-=k/q2;
while(xx<=&&k/q2)xx+=k/q2;
if(gcd(xx,qwq[i])==){printf("%lld\n",qwq[i]);return ;}
}
if(((-a-b)%q1==)){
ll xx,yy;
exgcd(n*k/qwq[i],k,xx,yy);
xx*=(-a-b)/q1;
while(xx>&&k/q1)xx-=k/q1;
while(xx<=&&k/q1)xx+=k/q1;
if(gcd(xx,qwq[i])==){printf("%lld\n",qwq[i]);return ;}
}
if((-abs(a-b))%q2==){
ll xx,yy;
exgcd(n*k/qwq[i],k,xx,yy);
xx*=-abs(a-b)/q2;
while(xx>&&k/q2)xx-=k/q2;
while(xx<=&&k/q2)xx+=k/q2;
if(gcd(xx,qwq[i])==){printf("%lld\n",qwq[i]);return ;}
}
}
}
return ;
}

[ Codeforces Round #549 (Div. 2)][D. The Beatles][exgcd]的更多相关文章

  1. Codeforces Round #549 (Div. 1)

    今天试图用typora写题解 真开心 参考 你会发现有很多都是参考的..zblzbl Codeforces Round #549 (Div. 1) 最近脑子不行啦 需要cf来缓解一下 A. The B ...

  2. [题解] Codeforces Round #549 (Div. 2) B. Nirvana

    Codeforces Round #549 (Div. 2) B. Nirvana [题目描述] B. Nirvana time limit per test1 second memory limit ...

  3. Codeforces Round #549 (Div. 2) 训练实录 (5/6)

    The Doors +0 找出输入的01数列里,0或者1先出完的的下标. Nirvana +3 输入n,求1到n的数字,哪个数逐位相乘的积最大,输出最大积. 思路是按位比较,从低到高,依次把小位换成全 ...

  4. Codeforces Round #549 (Div. 1) 题解

    link 前几天补完了某一场很早以前的div1,突然想来更博客,于是就有了这篇文章 A The Beatles 显然若起点和第一次到达的位置距离为 d ,那么经过的不同站点数为 $\frac{nk}{ ...

  5. Codeforces Round #549 (Div. 2) Solution

    传送门 A.The Doors 看懂题目就会写的题 给一个 $01$ 序列,找到最早的位置使得 $0$ 或 $1$ 已经全部出现 #include<iostream> #include&l ...

  6. Codeforces Round #549 (Div. 2) F 数形结合 + 凸包(新坑)

    https://codeforces.com/contest/1143/problem/F 题意 有n条形如\(y=x^2+bx+c\)的抛物线,问有多少条抛物线上方没有其他抛物线的交点 题解 \(y ...

  7. Codeforces Round #549 (Div. 2) E 倍增处理按排列顺序的上一个位置

    https://codeforces.com/contest/1143/problem/E 题意 p为n的一个排列,给出有m个数字的数组a,q次询问,每次询问a数组区间[l,r]中是否存在子序列为p的 ...

  8. Codeforces Round #549 (Div. 2) D 数学

    https://codeforces.com/contest/1143/problem/D 题意 有nk个城市,第1,k+1,2k+1,...,(n-1)k+1城市有餐厅,你每次能走l距离,a为起始位 ...

  9. CodeForces Round #549 Div.2

    A. The Doors 代码: #include <bits/stdc++.h> using namespace std; ; int N; , One = ; int a[maxn], ...

随机推荐

  1. 【Entity Framework】Revert the database to specified migration.

    本文涉及的相关问题,如果你的问题或需求有与下面所述相似之处,请阅读本文 [Entity Framework] Revert the database to specified migration. [ ...

  2. easyUI使用datagrid-detailview.js实现二级列表嵌套

    本文为博主原创,转载请注明: 在easyUI中使用datagrid-detailview.js可快速实现二级折叠列表,示例如下: 注意事项: 原本在谷歌浏览器进行示例测试的,url请求对应的json文 ...

  3. 廖雪峰JavaScript学习笔记(基础及数据类型、变量)

    先睹为快 alert('我要学JavaScript!'); Run: 基本语法: 1.每个语句以;结束,不强制 2.语句块用{...} 3.//单行注释,/*...*/ 多行注释 数据类型: 1.不区 ...

  4. python requests 上传文件

    token="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjoiZWM2ZWFjZjFjM2UzYzEyOWU3ODA4YjgwNzkxNGI ...

  5. Web开发中button与submit区别

    submit是button的一个特例,也是button的一种,它把提交这个动作自动集成了. 如果表单在点击提交按钮后需要用JS进行处理(包括输入验证)后再提交的话,通常都必须把submit改成butt ...

  6. ssh远程登陆和MTR测试

    ssh -p 22 root@142.234.255.66 which mtr yum install mtr -y mtr -c 20 -n --report www.baidu.com mtr - ...

  7. Django框架简介-模板系统

    2.4 模板 官方文档 2.4.1 常用语法 只需要记两种特殊符号: {{  }}和 {% %} 变量相关的用{{}},逻辑相关的用{%%}. 2.4.1.1 变量 {{ 变量名 }} 变量名由字母数 ...

  8. babel-plugin-import配置babel按需引入antd模块,编译后报错.bezierEasingMixin()

    用create-react-app做项目的时候,同时引入了antd,为了实现按需加载antd模块,用到他们提供的  babel-plugin-import  ( 一个用于按需加载组件代码和样式的 ba ...

  9. CRM INBOX 查询结果增强字段

    参考:https://blogs.sap.com/2013/03/25/how-to-integrate-new-result-list-attributes-into-the-agent-inbox ...

  10. ASP.NET后台输出js

    一.使用RegisterClientScriptBlock输出js代码块 StringBuilder sb = new StringBuilder(); sb.Append("<inp ...