zoukankan      html  css  js  c++  java
  • CodeForces 592C-Warrior and Archer(数论+python写法)

    题目链接:https://codeforces.com/problemset/problem/592/C
    CSDN食用链接:https://blog.csdn.net/qq_43906000/article/details/107831159

    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.
    https://vj.z180.cn/69d2b0570f996c92afb2f74c45ba9c73?v=1596554544
    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·10^{18})) — 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 (frac{p}{q}). Follow the format of the samples output.

    The fraction (frac{p}{q})( 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.

    题目大意:给你一个赛道的长度,终点是在1到赛道长度中随机选择的,终点之后是一个深渊,每次给你他们一步跨出的距离,问他们存活下来后平局的概率是多少。

    emmm,挺折腾人的这题。。实际上我们直接去找他们的最小公倍数就好了,然后在赛道(n)的长度下判断有多少个最小公倍数,假设可以存在(x)个最小公倍数,那么前(x-1)个最小公倍数之后一定会存在(min(a,b)-1)个数使得两个人都无法到达,而还需要考虑的是(0)之后和(x-1)个之后,(0)之后的话也是一样地加上(min(a,b)-1)个,(x-1)个的话就需要判断(lcm(a,b)*x+min(a,b)-1)是否大于总长了。

    用C++写的话大概就是这样的:(考虑到会爆long long,我们要使用__int128,将下面的代码中的ull替换为__int128再改下输入和输出就OK了)

    #include <bits/stdc++.h>
    using namespace std;
    
    typedef unsigned long long ull;
    typedef long long ll;
    const int mac=5e5+10;
    
    int a[300][300];
    
    int main(int argc, char const *argv[])
    {
    	ull t,a,b;
    	cin>>t>>a>>b;
    	if (a==b) {cout<<"1"<<"/"<<"1"<<endl; return 0;}
    	if (a==1 || b==1){
    		ull nb=t/max(a,b);
    		if (a==b) cout<<"1"<<"/"<<"1"<<endl;
    		else {
    			ull p=__gcd(t,nb);
    			cout<<nb/p<<"/"<<t/p<<endl;
    		}
    	}
    	else {
    		ull lcm=a/__gcd(a,b)*b;
    		ull nb=t/lcm;
    		ull fz=nb*min(a,b);
    		if (t>=lcm*nb+min(a,b)) fz+=min(a,b)-1;
    		else fz+=t-lcm*nb;
    		ull p=__gcd(t,fz);
    		cout<<fz/p<<"/"<<t/p<<endl;
    	}
    	return 0;
    }
    

    以下是python的AC代码:

    s=input().split(' ')
    t=int(s[0]); a=int(s[1]); b=int(s[2]);
    
    def gcd(a,b):
        if b==0:
            return a
        return gcd(b,a%b)
    
    if a==b:
        print("1/1")
        exit()
    
    if a==1 | b==1:
        nb=max(a,b)
        p=gcd(t,nb)
        print(nb//p,end='')
        print("/",end='')
        print(t//p)
    
    else :
        lcm=a//gcd(a,b)*b
        nb=t//lcm
        fz=nb*min(a,b)
        if t>=lcm*nb+min(a,b):
            fz=fz+min(a,b)-1
        else :
            fz=fz+(t-lcm*nb)
        p=gcd(t,fz)
        print(fz//p,end='')
        print("/",end='')
        print(t//p)
    
    
    路漫漫兮
  • 相关阅读:
    史上最全Html与CSS布局技巧
    Discuz! X的CSS加载机制
    关于input框只能输入纯数字问题
    对象的数据属性
    vue中将光标定位到Input上的问题
    端口占用问题解决方案
    el-button如何消除右边计数样式
    如何改变vscode的背景颜色
    为什么——要实例化对象?
    call()&apply()
  • 原文地址:https://www.cnblogs.com/lonely-wind-/p/13444300.html
Copyright © 2011-2022 走看看