第八届福建省赛Problem 2278 YYS(期望 + 大数)

链接 http://acm.fzu.edu.cn/problem.php?pid=2278

Problem Description
Yinyangshi is a famous RPG game on mobile phones.

Kim enjoys collecting cards in this game. Suppose there are n kinds of cards. If you want to get a new card, you need to pay W coins to draw a card. Each time you can only draw one card, all the cards appear randomly with same probability 1/n. Kim can get 1 coin each day. Suppose Kim has 0 coin and no cards on day 0. Every W days, Kim can draw a card with W coins. In this problem ,we define W=(n-1)!.

Now Kim wants to know the expected days he can collect all the n kinds of cards.

Input
The first line an integer T(1 ≤ T ≤ 10). There are T test cases.

The next T lines, each line an integer n. (1≤n≤3000)

Output
For each n, output the expected days to collect all the n kinds of cards, rounded to one decimal place.

Sample Input
4
1
2
5
9
Sample Output
1.0
3.0
274.0
1026576.0

题意
给你一个数n,问你集齐n张卡的期望天数?
条件:每(n-1)!天可以抽一次,每次抽到的卡片的概率是等可能事件。

思路
我们先确定要抽的期望次数
第i张卡抽齐的概率是(n-i+1)/n,则次数是n/(n-i+1),求和即可,然后乘以(n-1)!,化简使其方便计算就是(n!)/i的求和,数据规模得用java的大数处理。

代码

import java.util.*;
import java.math.*;

public class Main{
	static int N = 3005;
	static BigInteger f[] = new BigInteger[N];
	public static void init() {
		f[0] = BigInteger.ONE;
		for(int i=1;i<=3000;i++) {
			f[i]=f[i-1].multiply(BigInteger.valueOf(i));
		}
	}
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int Case = cin.nextInt();
		init();
		while(Case>0) {
			Case--;
			int n = cin.nextInt();
			BigInteger t = f[n],ans = BigInteger.ZERO;
			for(int i=1;i<=n;i++) {
				BigInteger tt = t.divide(BigInteger.valueOf(i));
				ans = ans.add(tt);
			}
			System.out.println(ans+".0");
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值