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");
    		}
    	}
    }                 
    

      

  • 相关阅读:
    Redis之使用python脚本监控队列长度
    ELK之filebate收集日志传递至Logstash
    [转] SOLID五大设计原则
    [转] 面向对象原则之GOF是招式,九大原则才是精髓
    [转] (CQRS)命令和查询责任分离架构模式(一) 之 什么是CQRS
    [0] 四色原型
    [0] C#软件项目版本号的命名规则及格式介绍
    [0] AssemblyInfo.cs文件介绍
    [0] 服务器 TCP 提供程序无法在 [ 'any' <ipv4> *] 上侦听。TCP 端口已在使用中。
    [0] C#异常种类
  • 原文地址:https://www.cnblogs.com/l609929321/p/9544230.html
Copyright © 2011-2022 走看看