2013长春网络赛 D 大数+gcd

版权声明:欢迎转载博客(转载时请附上原文链接^_^) https://blog.csdn.net/OneLine_/article/details/88067082

最近真的有点高产似那啥了_(:з」∠)_

昨天晚上组队赛的时候没想到要用JAVA大数做

直到比赛结束后 别的队伍说用JAVA做就能过 (有点森气自己没尝试JAVA)

这是一道可以面向答案推出公式的题目

(队友很大胆的猜测了 但是我们卡在了大数上 直接WA了)

上题目

D

MMM got a big big big cake, and invited all her M friends to eat the cake together. Surprisingly one of her friends HZ took some (N) strawberries which MMM likes very much to decorate the cake (of course they also eat strawberries, not just for decoration). HZ is in charge of the decoration, and he thinks that it's not a big deal that he put the strawberries on the cake randomly one by one. After that, MMM would cut the cake into M pieces of sector with equal size and shape (the last one came to the party will have no cake to eat), and choose one piece first. MMM wants to know the probability that she can get all N strawberries, can you help her? As the cake is so big, all strawberries on it could be treat as points. 

Input

First line is the integer T, which means there are T cases. 
For each case, two integers M, N indicate the number of her friends and the number of strawberry. 
(2 < M, N <= 20, T <= 400) 

Output

As the probability could be very small, you should output the probability in the form of a fraction in lowest terms. For each case, output the probability in a single line. Please see the sample for more details. 

Sample Input

2
3 3
3 4

Sample Output

1/3
4/27

很容易就推出公式 

 n / (m^(n-1))

kuangbin博客说是积分求得的 (讲真 我没有很懂 但是可以凭感觉推公式emmm)

附上巨佬的传送门:http://www.cnblogs.com/kuangbin/p/3346109.html 

刚刚用JAVA写了一发 AC了

上代码

PS:其中代码  @SuppressWarnings(value = { "all" }) 是用来注释掉警告的(某学长教我的)

import java.math.*;
import java.io.*;
import java.math.BigInteger;
import java.util.*;

//@SuppressWarnings(value = { "all" })

public class Main {
	public static BigInteger GCD(BigInteger a, BigInteger b) {
		if (b.compareTo(BigInteger.ZERO) == 0)
			return a;
		else
			return GCD(b, a.mod(b));
	}

	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int t = cin.nextInt();
		// BigInteger zero = BigInteger.valueOf(0);
		BigInteger one = BigInteger.valueOf(1);
		while (t-- > 0) {
			int n = cin.nextInt();
			int m = cin.nextInt();
			BigInteger N = BigInteger.valueOf(n);
			BigInteger M = BigInteger.valueOf(m);
			BigInteger b = BigInteger.ONE;
			for (int i = 1; i < m; i++) {
				b = b.multiply(N);
			}
			BigInteger k = GCD(b, M);
			b = b.divide(k);
			M = M.divide(k);
			System.out.println(M+"/"+b);
		}
	}
}

加油鸭~~

猜你喜欢

转载自blog.csdn.net/OneLine_/article/details/88067082