Codeforces Round #328 (Div. 2)C. The Big Race

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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 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?

Input

The first line of the input contains three integers tw 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 pand 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 16 or 7 are chosen as the length of the racetrack.

题意:

终点可以在1-t里面随便选择一个
终点之后都是坑(不能到坑里面去),然后有两个人在比赛,一个人一步走w米,一个人一步走b米,谁能不越过终点的情况,走的最远,就算谁赢
然后问你选择相等的概率是多少

实际上是找 [1,t]     范围内有多少个数满足

L(modw)L(modv)
,即满足 wx+r=vy+r 的个数.

两边都减去r,得到 

wx=vy=lcm(w,v)k

也就是说,我们只要求出 lcm(w,v) ,然后 [1,t]     范围内所有( lcm(w,v)k+r,r<min(w,v) )的个数就是我们要求的tie的个数,输出tie/t即可. 
有需要注意的地方:

  • lcm(w,k)k    和r同时为0的情况(也就是L=0的时候)应该去掉
  • 第11个测试TLE其实是运算过程中爆__int64了,导致gcd()函数参数为负数
  • 由于 lcm(w,v) 会爆__int64,我们可以先用double暂存,然后根据其与t的大小关系进行后续处理.

参考:http://blog.csdn.net/lincifer/article/details/49557779
#include
          
          
           
           
using namespace std;

long long gcd(long long x, long long y)
{
    if(!y) return x;
    return gcd(y, x%y);
}

int main(void)
{
    long long t, w, b;
    cin >> t >> w >> b;
    long long MIN = min(w, b);
    long long GCD = gcd(w, b);
    double tt = 1.0*w/GCD*b;
    long long tmp;
    if(tt > 5e18)
    {
        tmp = min(t, MIN-1);
    }
    else
    {
        long long temp = w/GCD*b;
        MIN--;
        tmp = (t/temp)+(t/temp)*MIN;
        tmp += min(t%temp, MIN);
    }
    GCD = gcd(tmp, t);
    cout << tmp/GCD << '/' << t/GCD <
           
           
          
          

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值