Codeforces Round #549 (Div. 2) D. The Beatles
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 1to 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 1-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.
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.
Print the two integers xx and yy.
2 3 1 1
1 6
3 2 0 0
1 3
1 10 5 3
5 5
In the first example the restaurants are located in the cities 1 and 4, the initial city ss could be 2, 3, 5, or 6. 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 2 (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 2, 4, or 6. 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.
题意 有一个环状排列的城市,相邻距离为1,编号从1到n*k,共n*k个,一个人从s号城市出发,每次走l长度的距离,现在有标号为1,t*k+1,t*(k+1)+1.....(n-1)k+1的城市存在饭店,由于一些原因,这个人忘记了s和l为多少,只记得距离起点s最近的饭店有a的距离,并且走了l长度后,距离此时最近的饭店有b距离,求这个人从s出发,再回到s所需要绕行整个圈的次数的最大值和最小值。
根据题意容易得到,两个饭店之间相距tk,就有t*k=±a±b+l (t为小于等于n的整数) 又有p*l=0 mod k,那么很明显,次数s=n*k/gcd(n*k,l) ,所以只要暴力一遍所有情况即可,复杂度O(4n)。
这个题目第一个坑点是n*k显然爆int,所以全开long long,第二个坑点是既然是long long 初始化最小值最好初始化到long long的上限,因为不涉及加减,所以初始化9e18也可以,不然你会发现你的最小值的初值不够大...
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> pii; int main() { ll n,k,a,b; cin>>n>>k>>a>>b; ll t[4]={a+b,a-b,-a+b,-a-b}; ll p=n*k; ll mx=-1,mn=1e18; for(int i=0;i<4;i++) { ll s=t[i]; for(int j=0;j<n;j++,s+=k) { mx=max(mx,p/(abs(__gcd(p,s)))); mn=min(mn,p/(abs(__gcd(p,s)))); } } cout<<mn<<" "<<mx<<endl; return 0; }