codeforces #328 C. The Big Race

3927 단어 codeforces
C. The Big Race time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output 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 L meters 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?
Input 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.
Output 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 p and q are divisible by d. Sample test(s) input 10 3 2 output 3/10 input 7 1 2 output 3/7 Note In the first sample Willman and Bolt will tie in case 1, 6 or 7 are chosen as the length of the racetrack. 사고방식: 경기가 불공평한 경우: 1. 둘 다 구덩이에 들어간다. 즉, 둘 다 공배수 2. 둘 다 구덩이에 들어가지 않는다. 즉, 1~min(w,b)-13. 길이 t가 공배수보다 작을 때sum=min(w,b)*(n/lcm)+min(n%lcm,min(w,b)-1).t가 공배수보다 크면sum=min(min(w,b)-1,n);주의1. 본 문제는 공배수를 구할 때 넘칠 수 있는 상황이 발생할 수 있으므로 먼저 w/gcd(w, b)*b가 공배수 2를 구해야 한다. w, b가 두 개의 큰 수일 수 있기 때문에 그 공배수는 틀림없이 넘칠 것이고 길이 t는 반드시 공배수보다 작을 것이다.따라서 n/w*gcd(w,b)/b로 t가 공배수보다 작은지 판단할 수 있다.
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cctype>
using namespace std;
typedef long long LL;
LL n, w, b, m, sum, d;
LL gcd(LL a, LL b) {
    return b == 0? a: gcd(b, a%b);
}
int main() {
    cin >> n >> w >> b;
    if (n/w*gcd(w, b)/b > 0) {
        LL lcm = w/gcd(w, b)*b;
        sum = min(w, b)*(n/lcm) + min(n%lcm, min(w, b)-1);
    } else {
        sum = min(min(w, b) - 1, n);
    }
    d = gcd(sum, n);
    printf("%lld/%lld
"
, sum/d, n/d); return 0; }

좋은 웹페이지 즐겨찾기