HDU-4762 Cut the Cake

来源:互联网 发布:java 实现 ping ip 编辑:程序博客网 时间:2024/06/05 17:00

题目:

http://acm.hdu.edu.cn/showproblem.php?pid=4762

题意:

一个蛋糕上随机放n个草莓,蛋糕要切成m份,每份大小形状完全一样。求小明先选的那块蛋糕上有所有草莓的概率。

思路:

就是推公式加高精度。

首先以其中一个草莓为标准,就有n种可能,其他草莓在这个草莓所在那块的概率是1/m。

总的概率就是 n*(m^(n-1)).

然后直接高精度求解即可。

然而对于我来说到这里才懵了,,毕竟java不熟,,大数模版套过来还要求gcd化简分数,写出来真真是累。

于是还是老老实实去又学了学java的高精度。

代码:

import java.io.PrintWriter;import java.math.BigInteger;import java.util.Scanner;public class Main {    static BigInteger gcd(BigInteger a,BigInteger b){        if(!b.equals(BigInteger.ZERO)) return gcd(b,a.mod(b));        return a;    }    public static void main(String args[]){        Scanner cin=new Scanner(System.in);        int n,m;        BigInteger a,b,c;        int T = cin.nextInt();        while(T>0)        {        T--;            m = cin.nextInt();            n = cin.nextInt();        a = BigInteger.valueOf(m);        b = BigInteger.valueOf(n);        a = a.pow(n-1);        c = gcd(a,b);        System.out.println(b.divide(c)+"/"+a.divide(c));        }    }}










0 0