Vector Willman and Array Bolt are the two most famous athletes of Byteforces. They are going to compete in a race with a distance of Lmeters today.
Willman and Bolt have exactly the same speed, so when they compete the result is always a tie. That is a problem for the organizers because they want a winner.
While watching previous races the organizers have noticed that Willman can perform only steps of length equal to w meters, and Bolt can perform only steps of length equal to b meters. Organizers decided to slightly change the rules of the race. Now, at the end of the racetrack there will be an abyss, and the winner will be declared the athlete, who manages to run farther from the starting point of the the racetrack (which is not the subject to change by any of the athletes).
Note that none of the athletes can run infinitely far, as they both will at some moment of time face the point, such that only one step further will cause them to fall in the abyss. In other words, the athlete will not fall into the abyss if the total length of all his steps will be less or equal to the chosen distance L.
Since the organizers are very fair, the are going to set the length of the racetrack as an integer chosen randomly and uniformly in range from 1 to t (both are included). What is the probability that Willman and Bolt tie again today?
The first line of the input contains three integers t, w and b (1 ≤ t, w, b ≤ 5·1018) — the maximum possible length of the racetrack, the length of Willman's steps and the length of Bolt's steps respectively.
Print the answer to the problem as an irreducible fraction . Follow the format of the samples output.
The fraction (p and q are integers, and both p ≥ 0 and q > 0 holds) is called irreducible, if there is no such integer d > 1, that both pand q are divisible by d.
10 3 2
3/10
7 1 2
3/7
In the first sample Willman and Bolt will tie in case 1, 6 or 7 are chosen as the length of the racetrack.
题意:给你一个数t,问你1- t里面有多少个D满足 D % w = D % b。
思路:M = lcm(w, b)。
(1)当M > t时,有ans = min(t, min(w-1, b-1))。
(2)当M <= t时,考虑每个lcm(a, b)做出的贡献,可以推出每个lcm(a, b)会出现min(w, b)个符合的数。这样, 我们只需统计lcm(a, b)在t里面的个数,最后剩余的数按第一种情况处理就好了。
注意:求解lcm的时候,long long会溢出,可以用double存储。
AC代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#define MAXN 101000
#define LL long long
#define Ri(a) scanf("%d", &a)
#define Pi(a) printf("%d\n", (a))
#define Rl(a) scanf("%lld", &a)
#define Pl(a) printf("%lld\n", (a))
#define Rs(a) scanf("%s", a)
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define INF 0x3f3f3f3f
using namespace std;
LL gcd(LL a, LL b){
return b == 0 ? a : gcd(b, a%b);
}
int main()
{
LL t, w, b;
Rl(t), Rl(w), Rl(b);
LL GCD = gcd(w, b);
LL ans;
if(w * 1.0 / GCD * b > (double)t)
ans = min(t, min(w-1, b-1));
else
{
LL LCM = w / GCD * b;
LL num = t / LCM;
LL remain = t - num * LCM;
ans = num * min(w, b) + min(remain, min(w-1, b-1));
}
GCD = gcd(ans, t);
printf("%lld/%lld\n", ans/GCD, t/GCD);
return 0;
}