• CF#328 (Div. 2) C(大数)


    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 onlysteps of length equal to w meters, and Bolt can perform only steps of length equal to bmeters. 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 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.

    题目看了半天啊!!!看懂了算法很好想,可是要用到大数,电脑没装eclipse,gvim写java也是醉醉的,还好最后过的还不算晚。贴一下代码留着以后参考,后面还要仔细整理下BigInteger,BigDecimal

    /*
    ID: LinKArftc
    PROG: Main.java
    LANG: JAVA
    */
    import java.util.*;
    import java.math.*;
    public class Main {
        public static void main(String[] args) {
            BigInteger t, w, b;
            Scanner input = new Scanner(System.in);
            while (input.hasNext()) {
                t = input.nextBigInteger();
                w = input.nextBigInteger();
                b = input.nextBigInteger();
                BigInteger mi = w.min(b);
                BigInteger gcd = w.gcd(b);
                BigInteger lcm = w.multiply(b).divide(gcd);
                BigInteger ans, cnt, res;
                if (lcm.compareTo(t) > 0) {
                    ans = t.min(w.subtract(BigInteger.ONE).min(b.subtract(BigInteger.ONE)));
                } else {
                    cnt = t.divide(lcm);
                    res = t.subtract(cnt.multiply(lcm));
                    ans = cnt.multiply(w.min(b)).add(res.min((w.subtract(BigInteger.ONE)).min(b.subtract(BigInteger.ONE))));
                }
                gcd = ans.gcd(t);
                System.out.println(ans.divide(gcd)+"/"+t.divide(gcd));
                
            }
        }
    }

     再贴一发Python3代码

    import sys
    import fractions
    for line in sys.stdin:
        li = line.split()
        t = int(li[0])
        w = int(li[1])
        b = int(li[2])
        mi = min(w, b)
        Gcd = fractions.gcd(w, b)
        Lcm = w * b // Gcd
        if Lcm > t:
            ans = min(t, w - 1, b - 1)
        else:
            cnt = t // Lcm
            res = t - cnt * Lcm
            ans = cnt * min(w, b) + min(res, w - 1, b - 1)
        Gcd = fractions.gcd(ans, t)
        print("%s/%s" % (ans // Gcd, t // Gcd))
  • 相关阅读:
    python基础之元组、文件操作、编码、函数、变量
    python---基础之模块,列表,元组,字典
    python成长之路-----day1-----作业(登录程序和三级菜单)
    k8s的port、targetport、nodeport之间的区别
    查找并删除文件
    systemctl自定义service
    中标麒麟7.0源
    springboot问题,没有主清单属性
    iso搭建本地源
    添加路由
  • 原文地址:https://www.cnblogs.com/LinKArftc/p/4926636.html
Copyright © 2020-2023  润新知