题目链接:
题目描写叙述:现有一个大蛋糕。上面随机分布了n个草莓,然后将草莓切成m块,问n个草莓全在一块蛋糕上面的概率?
解题思路:细致分析可得:C(n,1)/m^(n-1)
因为m<=20,n<=20所以用long long 表示是会溢出的,所以考虑大整数操作这里能够用java
代码:
java
import java.math.BigInteger;import java.util.Scanner;public class Main { public static void main(String[] args){ int T; int n,m; Scanner cin= new Scanner(System.in); T=cin.nextInt(); while(T>0){ m = cin.nextInt(); n = cin.nextInt(); BigInteger t1=BigInteger.valueOf(n); BigInteger t2=BigInteger.valueOf(m).pow(n-1); BigInteger tt=t1.gcd(t2); t1=t1.divide(tt); t2=t2.divide(tt); System.out.println(t1+"/"+t2); T--; } cin.close(); }}
C++
#pragma comment(linker, "/STACK:1024000000,1024000000")#include #include #include #include #include #include #include #include