2013长春网络赛 D 大数+gcd

博客探讨了一道数学概率题目的解法,通过面向答案推导公式,并使用JAVA大数处理来解决大数运算的问题,确保计算精度。文章分享了具体公式推导过程及JAVA代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

昨天晚上组队赛的时候没想到要用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);
		}
	}
}

加油鸭~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值