HDU4762(JAVA大数)

Cut the Cake

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1102    Accepted Submission(s): 540

Problem Description

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

题意:切M块蛋糕,求N个草莓全部在一块蛋糕上的概率。

公式:n/(m的n-1次方);

收获:了解了下JAVA大数。化简可以用两个数的最小公倍数。

import java.math.*;
import java.util.*;
public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        while(t-->0)
        {
            BigInteger m = in.nextBigInteger();
            int n = in.nextInt();
            m=m.pow(n-1);
            BigInteger b=BigInteger.valueOf(n);
            BigInteger a;
            a = m.gcd(b);
            BigInteger c = m.divide(a);
            BigInteger c2 = b.divide(a);
            System.out.println(c2 + "/" + c);
        }

    }

}
时间: 2024-11-02 15:00:20

HDU4762(JAVA大数)的相关文章

nyoj 73 比大小 【java大数】

java大数. 代码: import java.util.Scanner; import java.math.*; public class Main{ public static void main(String[] args){ Scanner cin = new Scanner(System.in); BigInteger a, b; BigInteger t = BigInteger.valueOf(0); a = cin.nextBigInteger(); b = cin.nextBi

UVA10303 - How Many Trees?(java大数+catalan数)

UVA10303 - How Many Trees?(java大数+catalan数) 题目链接 题目大意:给你1-N N个数,然后要求由这些数构成二叉搜索树,问有多少种这样的二叉搜索树. 解题思路:把前5项理出来,正好是1 2 5 14 42..就猜想是catalan数,结果也是对了.公式f(i + 1) = (4?i - 6)/ i; (i >= 2).结果很大,要用高精度. 代码: import java.util.*; import java.math.*; import java.io

UVA10183 - How Many Fibs?(java大数+二分)

UVA10183 - How Many Fibs?(java大数+二分) 题目链接 题目大意:给你a,b,求[a,b]之间有多少个fibs数. 解题思路:虽然a.b很大,但是在10^100内的fibs却不超过500个.这样就可以先把这些fibs保存在数组中,然后每次二分去找a,b的位置,然后就可以得到之间有多少个fibs. 代码: import java.util.*; import java.math.*; import java.io.*; import java.lang.String.*

hdu4927 Series 1(组合+公式 Java大数高精度运算)

题目链接: Series 1 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 423    Accepted Submission(s): 146 Problem Description Let A be an integral series {A1, A2, . . . , An}. The zero-order series o

多校第六场 HDU 4927 JAVA大数类

题目大意:给定一个长度为n的序列a,每次生成一个新的序列,长度为n-1,新序列b中bi=ai+1?ai,直到序列长度为1.输出最后的数. 思路:这题实在是太晕了,比赛的时候搞了四个小时,从T到WA,唉--对算组合还是不太了解啊,现在对组合算比较什么了-- import java.io.*; import java.math.*; import java.util.*; public class Main { public static void main(String[] args) { Sca

java大数的基本函数

1.读入 Scanner cin=new Scanner(System.in);// 读入 while(cin.hasNextInt()) //等同于!=EOF,第一数一定要输入整形的 { } 大数的一般是: while(cin.hasNextBigInteger())  //第一个数一定要输入大数的 { } while(t-->0)   //等同于while(t--) { } 2.赋值 BigInteger b=BigInteger.valueOf(a); //a可为int,long,stri

ACM-ICPC java(大数)使用总结

今天碰到一道大数除法和模运算的题,以前也写过加减乘的大数模拟运算,但总觉着太麻烦了,今天大体了解了一下Java的输入输出,特来总结一下如何使用java中的高精度类型.首先我们要会建一个简单的java程序(以A+B为例)如下 import java.io.*; import java.util.*; import java.math.*; import java.text.*; public class Main { public static void main(String args[]) {

HDU 4927 Series 1 java大数

java mle前会wa 或者 t 这种事我会乱说? import java.math.*; import java.util.*; import java.io.*; public class Main { BigInteger[] a = new BigInteger[3007]; public void work() { int T; T = cin.nextInt(); while (T-- > 0) { int n; n = cin.nextInt(); for (int i = 0;

Codeforces 18D Seller Bob java大数+贪心

题目链接:点击打开链接 java: import java.math.BigInteger; import java.util.Scanner; public class Main { static int N = 5005; static BigInteger[] er = new BigInteger[N]; static BigInteger E = new BigInteger("2"); static int[] a = new int[N]; static int[] ma