zoukankan      html  css  js  c++  java
  • 2017福建省赛 FZU 2278 YYS 数学 大数

    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-1)!天你可以抽一张卡片,抽中每张卡片的概率为1/n,现在你要抽取n张卡片,期望是多少?
    分析:假设有n张卡片,你已经抽到了k张不同的卡片,则抽中(k+1)张不同的概率为:(n-k)/n,所以抽中(k+1)张的期望次数为n/(n-k),也就是平均抽n/(n-k)张才能抽到一张不同的卡片,期望即是平均值
       即 E(k+1) = n/(n-k)
       所以期望 E = (E(1)+E(2)+...+E(n))*(n-1)! = (n/n+n/(n-1)+...+n/1)*(n-1)! = n!/n + n!/(n-1) + ... + n!/1
    参考博客:https://blog.csdn.net/hnust_xx/article/details/75807071
    AC代码:
    import java.math.BigInteger;
    import java.util.Scanner;
    
    public class Main {
    	public static void main(String[] args) {
    		BigInteger [] a = new BigInteger [3010];
    		a[1] = BigInteger.valueOf(1);
    		for( int i = 2; i <= 3000; i ++ ) {
    			a[i] = a[i-1].multiply(BigInteger.valueOf(i));
    		}
    		Scanner cin = new Scanner(System.in);
    		int T;
    		T = cin.nextInt();
    		while( T > 0 ) {
    			T --;
    			int n;
    			n = cin.nextInt();
    			BigInteger sum = BigInteger.valueOf(0);
    			for( int i = 1; i <= n; i ++ ) {
    				sum = sum.add(a[n].divide(BigInteger.valueOf(i)));
    			}
    			System.out.print(sum);
    			System.out.println(".0");
    		}
    	}
    }                 
    

      

  • 相关阅读:
    10 个深恶痛绝的 Java 异常。。
    为什么公司宁愿 25K 重新招人,也不给你加到 20K?原因太现实……
    推荐一款代码神器,代码量至少省一半!
    Spring Cloud Greenwich 正式发布,Hystrix 即将寿终正寝。。
    hdu 3853 LOOPS(概率 dp 期望)
    hdu 5245 Joyful(期望的计算,好题)
    hdu 4336 Card Collector(期望 dp 状态压缩)
    hdu 4405 Aeroplane chess(概率+dp)
    hdu 5036 Explosion(概率期望+bitset)
    hdu 5033 Building (单调栈 或 暴力枚举 )
  • 原文地址:https://www.cnblogs.com/l609929321/p/9544230.html
Copyright © 2011-2022 走看看