Codeforces 592C The Big Race【数学啊】

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 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.

Examples
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.


题目大意:

一共有N个格子。谁跑的远谁赢,对应一个人每次跑a单位长度,另外一个人跑b单位长度,问不能决定胜负的概率。

其实就是求【1~t】中,数字x%a==x%b的概率。


思路:

1、首先问题肯定是相关Lcm辣~,他也肯定有周期啦~那么我们暴力打一波表看看吧~



那么不难发现,结果就是:a-1+n/l+(n/l-1)*(a-1)+min(a-1,n%l);(a-1是最上边的部分,n/l是整除部分,(n/l-1)*(a-1)是整除部分之后的循环次数部分,每次都循环a-1次,所以我们统计一共有几个整的a-1,累加起来,min(a-1,n%l)是用来判断是否有尾数存在的计算。)


2、接下来我们分类讨论(我们每次输入的时候都保证a比b小的情况下):

①如果a==b,很明显结果是1/1

②否则如果n<a,那么很明显结果也是1/1.

③如果以上条件都不满足,那么肯定有a-1个答案,那么接下来继续讨论

④如果此时a,b的Lcm小于了n,那么终止算法,结果就是a-1,否则继续。

⑤如果此时a,b的Lcm不小于n,那么对应我们的结果就是a-1+n/l+(n/l-1)*(a-1)+min(a-1,n%l);


3、这种题细节比较多。好烦啊..............


Ac代码:

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
#define ll __int64
ll gcd(ll x,ll y)
{
    return y==0?x:gcd(y,x%y);
}
int main()
{
    ll n,a,b;
    while(~scanf("%I64d%I64d%I64d",&n,&a,&b))
    {
                if(a>b)swap(a,b);
        if(a==b)
        {
            printf("1/1\n");
        }
        else
        {
            if(n<a)
            {
                printf("1/1\n",n);
            }
            else
            {
                ll g=gcd(a,b);
                ll output;
                if((n)/a*g<b)
                {
                    output=a-1;
                }
                else
                {
                    ll l=a/g*b;
                    output=a-1;
                    output+=n/l+(n/l-1)*(a-1)+min(a-1,n%l);
                }
                printf("%I64d/%I64d\n",output/gcd(output,n),n/gcd(output,n));
            }
        }
    }
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值