第八届福建省大学生程序设计竞赛 G.YYS【期望+大数】

 Problem 2278 YYS

Accept: 21    Submit: 75
Time Limit: 1000 mSec    Memory Limit : 262144 KB

 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

 Source

第八届福建省大学生程序设计竞赛-重现赛(感谢承办方厦门理工学院)

题目大意:

我们现在想要收集到n个卡片,现在已知抽到每种卡片的概率为1/n;

我们每隔(n-1)!天就可以进行一次抽奖。

问收集齐所有卡片的期望天数。


思路:


挂上dalao对这类题的理解:http://blog.csdn.net/qq_34374664/article/details/63260392


我们假设已经有了a张卡片的话,如果我们想要得到第a+1张,我们抽中他的概率显然就是(n-a)/n,只要我们得到除了这a张卡片的任意一张即可。那么从a张卡片变成a+1张卡片的期望抽奖次数是多少呢?


我们考虑这样一个问题,假设我们抽奖中奖的概率为1/4,那么期望中奖的抽奖次数是多少呢?显然是1/(1/4)=4.


那么回归到当前问题,如果我们已经有了a张卡片,想要得到第a+1张,我们抽中一张新的卡片概率很显然是(n-a)/n,那么期望进行的次数就是n/(n-a);


将这个问题分成n个独立事件去考虑,那么总期望E=各个单独事件的期望和;


那么Ans=Σn!/i(1<=i<=n)


这里n很大,需要写一个大数乘法;


懒了,直接拉了置顶模板(真的好长。。。。。。。)


Ac代码:

#include <iostream>
#include <cstring>
using namespace std;
#define DIGIT   4      //四位隔开,即万进制
#define DEPTH   10000        //万进制
#define MAX     25100    //题目最大位数/4,要不大直接设为最大位数也行
typedef int bignum_t[MAX+1];
int read(bignum_t a,istream&is=cin)
{
    char buf[MAX*DIGIT+1],ch ;
    int i,j ;
    memset((void*)a,0,sizeof(bignum_t));
    if(!(is>>buf))return 0 ;
    for(a[0]=strlen(buf),i=a[0]/2-1;i>=0;i--)
    ch=buf[i],buf[i]=buf[a[0]-1-i],buf[a[0]-1-i]=ch ;
    for(a[0]=(a[0]+DIGIT-1)/DIGIT,j=strlen(buf);j<a[0]*DIGIT;buf[j++]='0');
    for(i=1;i<=a[0];i++)
    for(a[i]=0,j=0;j<DIGIT;j++)
    a[i]=a[i]*10+buf[i*DIGIT-1-j]-'0' ;
    for(;!a[a[0]]&&a[0]>1;a[0]--);
    return 1 ;
}

void write(const bignum_t a,ostream&os=cout)
{
    int i,j ;
    for(os<<a[i=a[0]],i--;i;i--)
    for(j=DEPTH/10;j;j/=10)
    os<<a[i]/j%10 ;
}

int comp(const bignum_t a,const bignum_t b)
{
    int i ;
    if(a[0]!=b[0])
    return a[0]-b[0];
    for(i=a[0];i;i--)
    if(a[i]!=b[i])
    return a[i]-b[i];
    return 0 ;
}

int comp(const bignum_t a,const int b)
{
    int c[12]=
    {
        1
    }
    ;
    for(c[1]=b;c[c[0]]>=DEPTH;c[c[0]+1]=c[c[0]]/DEPTH,c[c[0]]%=DEPTH,c[0]++);
    return comp(a,c);
}

int comp(const bignum_t a,const int c,const int d,const bignum_t b)
{
    int i,t=0,O=-DEPTH*2 ;
    if(b[0]-a[0]<d&&c)
    return 1 ;
    for(i=b[0];i>d;i--)
    {
        t=t*DEPTH+a[i-d]*c-b[i];
        if(t>0)return 1 ;
        if(t<O)return 0 ;
    }
    for(i=d;i;i--)
    {
        t=t*DEPTH-b[i];
        if(t>0)return 1 ;
        if(t<O)return 0 ;
    }
    return t>0 ;
}
void add(bignum_t a,const bignum_t b)
{
    int i ;
    for(i=1;i<=b[0];i++)
    if((a[i]+=b[i])>=DEPTH)
    a[i]-=DEPTH,a[i+1]++;
    if(b[0]>=a[0])
    a[0]=b[0];
    else
    for(;a[i]>=DEPTH&&i<a[0];a[i]-=DEPTH,i++,a[i]++);
    a[0]+=(a[a[0]+1]>0);
}
void add(bignum_t a,const int b)
{
    int i=1 ;
    for(a[1]+=b;a[i]>=DEPTH&&i<a[0];a[i+1]+=a[i]/DEPTH,a[i]%=DEPTH,i++);
    for(;a[a[0]]>=DEPTH;a[a[0]+1]=a[a[0]]/DEPTH,a[a[0]]%=DEPTH,a[0]++);
}
void sub(bignum_t a,const bignum_t b)
{
    int i ;
    for(i=1;i<=b[0];i++)
    if((a[i]-=b[i])<0)
    a[i+1]--,a[i]+=DEPTH ;
    for(;a[i]<0;a[i]+=DEPTH,i++,a[i]--);
    for(;!a[a[0]]&&a[0]>1;a[0]--);
}
void sub(bignum_t a,const int b)
{
    int i=1 ;
    for(a[1]-=b;a[i]<0;a[i+1]+=(a[i]-DEPTH+1)/DEPTH,a[i]-=(a[i]-DEPTH+1)/DEPTH*DEPTH,i++);
    for(;!a[a[0]]&&a[0]>1;a[0]--);
}

void sub(bignum_t a,const bignum_t b,const int c,const int d)
{
    int i,O=b[0]+d ;
    for(i=1+d;i<=O;i++)
    if((a[i]-=b[i-d]*c)<0)
    a[i+1]+=(a[i]-DEPTH+1)/DEPTH,a[i]-=(a[i]-DEPTH+1)/DEPTH*DEPTH ;
    for(;a[i]<0;a[i+1]+=(a[i]-DEPTH+1)/DEPTH,a[i]-=(a[i]-DEPTH+1)/DEPTH*DEPTH,i++);
    for(;!a[a[0]]&&a[0]>1;a[0]--);
}
void mul(bignum_t c,const bignum_t a,const bignum_t b)
{
    int i,j ;
    memset((void*)c,0,sizeof(bignum_t));
    for(c[0]=a[0]+b[0]-1,i=1;i<=a[0];i++)
    for(j=1;j<=b[0];j++)
    if((c[i+j-1]+=a[i]*b[j])>=DEPTH)
    c[i+j]+=c[i+j-1]/DEPTH,c[i+j-1]%=DEPTH ;
    for(c[0]+=(c[c[0]+1]>0);!c[c[0]]&&c[0]>1;c[0]--);
}
void mul(bignum_t a,const int b)
{
    int i ;
    for(a[1]*=b,i=2;i<=a[0];i++)
    {
        a[i]*=b ;
        if(a[i-1]>=DEPTH)
        a[i]+=a[i-1]/DEPTH,a[i-1]%=DEPTH ;
    }
    for(;a[a[0]]>=DEPTH;a[a[0]+1]=a[a[0]]/DEPTH,a[a[0]]%=DEPTH,a[0]++);
    for(;!a[a[0]]&&a[0]>1;a[0]--);
}

void mul(bignum_t b,const bignum_t a,const int c,const int d)
{
    int i ;
    memset((void*)b,0,sizeof(bignum_t));
    for(b[0]=a[0]+d,i=d+1;i<=b[0];i++)
    if((b[i]+=a[i-d]*c)>=DEPTH)
    b[i+1]+=b[i]/DEPTH,b[i]%=DEPTH ;
    for(;b[b[0]+1];b[0]++,b[b[0]+1]=b[b[0]]/DEPTH,b[b[0]]%=DEPTH);
    for(;!b[b[0]]&&b[0]>1;b[0]--);
}
void div(bignum_t c,bignum_t a,const bignum_t b)
{
    int h,l,m,i ;
    memset((void*)c,0,sizeof(bignum_t));
    c[0]=(b[0]<a[0]+1)?(a[0]-b[0]+2):1 ;
    for(i=c[0];i;sub(a,b,c[i]=m,i-1),i--)
    for(h=DEPTH-1,l=0,m=(h+l+1)>>1;h>l;m=(h+l+1)>>1)
    if(comp(b,m,i-1,a))h=m-1 ;
    else l=m ;
    for(;!c[c[0]]&&c[0]>1;c[0]--);
    c[0]=c[0]>1?c[0]:1 ;
}

void div(bignum_t a,const int b,int&c)
{
    int i ;
    for(c=0,i=a[0];i;c=c*DEPTH+a[i],a[i]=c/b,c%=b,i--);
    for(;!a[a[0]]&&a[0]>1;a[0]--);
}
void sqrt(bignum_t b,bignum_t a)
{
    int h,l,m,i ;
    memset((void*)b,0,sizeof(bignum_t));
    for(i=b[0]=(a[0]+1)>>1;i;sub(a,b,m,i-1),b[i]+=m,i--)
    for(h=DEPTH-1,l=0,b[i]=m=(h+l+1)>>1;h>l;b[i]=m=(h+l+1)>>1)
    if(comp(b,m,i-1,a))h=m-1 ;
    else l=m ;
    for(;!b[b[0]]&&b[0]>1;b[0]--);
    for(i=1;i<=b[0];b[i++]>>=1);
}
int digit(const bignum_t a,const int b)
{
    int i,ret ;
    for(ret=a[(b-1)/DIGIT+1],i=(b-1)%DIGIT;i;ret/=10,i--);
    return ret%10 ;
}
#define SGN(x) ((x)>0?1:((x)<0?-1:0))
#define ABS(x) ((x)>0?(x):-(x))

int read(bignum_t a,int&sgn,istream&is=cin)
{
    char str[MAX*DIGIT+2],ch,*buf ;
    int i,j ;
    memset((void*)a,0,sizeof(bignum_t));
    if(!(is>>str))return 0 ;
    buf=str,sgn=1 ;
    if(*buf=='-')sgn=-1,buf++;
    for(a[0]=strlen(buf),i=a[0]/2-1;i>=0;i--)
    ch=buf[i],buf[i]=buf[a[0]-1-i],buf[a[0]-1-i]=ch ;
    for(a[0]=(a[0]+DIGIT-1)/DIGIT,j=strlen(buf);j<a[0]*DIGIT;buf[j++]='0');
    for(i=1;i<=a[0];i++)
    for(a[i]=0,j=0;j<DIGIT;j++)
    a[i]=a[i]*10+buf[i*DIGIT-1-j]-'0' ;
    for(;!a[a[0]]&&a[0]>1;a[0]--);
    if(a[0]==1&&!a[1])sgn=0 ;
    return 1 ;
}
struct bignum
{
    bignum_t num ;
    int sgn ;
    public :
    inline bignum()
    {
        memset(num,0,sizeof(bignum_t));
        num[0]=1 ;
        sgn=0 ;
    }
    inline int operator!()
    {
        return num[0]==1&&!num[1];
    }
    inline bignum&operator=(const bignum&a)
    {
        memcpy(num,a.num,sizeof(bignum_t));
        sgn=a.sgn ;
        return*this ;
    }
    inline bignum&operator=(const int a)
    {
        memset(num,0,sizeof(bignum_t));
        num[0]=1 ;
        sgn=SGN (a);
        add(num,sgn*a);
        return*this ;
    }
    ;
    inline bignum&operator+=(const bignum&a)
    {
        if(sgn==a.sgn)add(num,a.num);
        else if
        (sgn&&a.sgn)
        {
            int ret=comp(num,a.num);
            if(ret>0)sub(num,a.num);
            else if(ret<0)
            {
                bignum_t t ;
                memcpy(t,num,sizeof(bignum_t));
                memcpy(num,a.num,sizeof(bignum_t));
                sub (num,t);
                sgn=a.sgn ;
            }
            else memset(num,0,sizeof(bignum_t)),num[0]=1,sgn=0 ;
        }
        else if(!sgn)
            memcpy(num,a.num,sizeof(bignum_t)),sgn=a.sgn ;
        return*this ;
    }
    inline bignum&operator+=(const int a)
    {
        if(sgn*a>0)add(num,ABS(a));
        else if(sgn&&a)
        {
            int  ret=comp(num,ABS(a));
            if(ret>0)sub(num,ABS(a));
            else if(ret<0)
            {
                bignum_t t ;
                memcpy(t,num,sizeof(bignum_t));
                memset(num,0,sizeof(bignum_t));
                num[0]=1 ;
                add(num,ABS (a));
                sgn=-sgn ;
                sub(num,t);
            }
            else memset(num,0,sizeof(bignum_t)),num[0]=1,sgn=0 ;
        }
        else if
            (!sgn)sgn=SGN(a),add(num,ABS(a));
        return*this ;
    }
    inline bignum operator+(const bignum&a)
    {
        bignum ret ;
        memcpy(ret.num,num,sizeof (bignum_t));
        ret.sgn=sgn ;
        ret+=a ;
        return ret ;
    }
    inline bignum operator+(const int a)
    {
        bignum ret ;
        memcpy(ret.num,num,sizeof (bignum_t));
        ret.sgn=sgn ;
        ret+=a ;
        return ret ;
    }
    inline bignum&operator-=(const bignum&a)
    {
        if(sgn*a.sgn<0)add(num,a.num);
        else if
        (sgn&&a.sgn)
        {
            int ret=comp(num,a.num);
            if(ret>0)sub(num,a.num);
            else if(ret<0)
            {
                bignum_t t ;
                memcpy(t,num,sizeof(bignum_t));
                memcpy(num,a.num,sizeof(bignum_t));
                sub(num,t);
                sgn=-sgn ;
            }
            else memset(num,0,sizeof(bignum_t)),num[0]=1,sgn=0 ;
        }
        else if(!sgn)add (num,a.num),sgn=-a.sgn ;
        return*this ;
    }
    inline bignum&operator-=(const int a)
    {
        if(sgn*a<0)add(num,ABS(a));
        else if(sgn&&a)
        {
            int  ret=comp(num,ABS(a));
            if(ret>0)sub(num,ABS(a));
            else if(ret<0)
            {
                bignum_t t ;
                memcpy(t,num,sizeof(bignum_t));
                memset(num,0,sizeof(bignum_t));
                num[0]=1 ;
                add(num,ABS(a));
                sub(num,t);
                sgn=-sgn ;
            }
            else memset(num,0,sizeof(bignum_t)),num[0]=1,sgn=0 ;
        }
        else if
            (!sgn)sgn=-SGN(a),add(num,ABS(a));
        return*this ;
    }
    inline bignum operator-(const bignum&a)
    {
        bignum ret ;
        memcpy(ret.num,num,sizeof(bignum_t));
        ret.sgn=sgn ;
        ret-=a ;
        return ret ;
    }
    inline bignum operator-(const int a)
    {
        bignum ret ;
        memcpy(ret.num,num,sizeof(bignum_t));
        ret.sgn=sgn ;
        ret-=a ;
        return ret ;
    }
    inline bignum&operator*=(const bignum&a)
    {
        bignum_t t ;
        mul(t,num,a.num);
        memcpy(num,t,sizeof(bignum_t));
        sgn*=a.sgn ;
        return*this ;
    }
    inline bignum&operator*=(const int a)
    {
        mul(num,ABS(a));
        sgn*=SGN(a);
        return*this ;
    }
    inline bignum operator*(const bignum&a)
    {
        bignum ret ;
        mul(ret.num,num,a.num);
        ret.sgn=sgn*a.sgn ;
        return ret ;
    }
    inline bignum operator*(const int a)
    {
        bignum ret ;
        memcpy(ret.num,num,sizeof (bignum_t));
        mul(ret.num,ABS(a));
        ret.sgn=sgn*SGN(a);
        return ret ;
    }
    inline bignum&operator/=(const bignum&a)
    {
        bignum_t t ;
        div(t,num,a.num);
        memcpy (num,t,sizeof(bignum_t));
        sgn=(num[0]==1&&!num[1])?0:sgn*a.sgn ;
        return*this ;
    }
    inline bignum&operator/=(const int a)
    {
        int t ;
        div(num,ABS(a),t);
        sgn=(num[0]==1&&!num [1])?0:sgn*SGN(a);
        return*this ;
    }
    inline bignum operator/(const bignum&a)
    {
        bignum ret ;
        bignum_t t ;
        memcpy(t,num,sizeof(bignum_t));
        div(ret.num,t,a.num);
        ret.sgn=(ret.num[0]==1&&!ret.num[1])?0:sgn*a.sgn ;
        return ret ;
    }
    inline bignum operator/(const int a)
    {
        bignum ret ;
        int t ;
        memcpy(ret.num,num,sizeof(bignum_t));
        div(ret.num,ABS(a),t);
        ret.sgn=(ret.num[0]==1&&!ret.num[1])?0:sgn*SGN(a);
        return ret ;
    }
    inline bignum&operator%=(const bignum&a)
    {
        bignum_t t ;
        div(t,num,a.num);
        if(num[0]==1&&!num[1])sgn=0 ;
        return*this ;
    }
    inline int operator%=(const int a)
    {
        int t ;
        div(num,ABS(a),t);
        memset(num,0,sizeof (bignum_t));
        num[0]=1 ;
        add(num,t);
        return t ;
    }
    inline bignum operator%(const bignum&a)
    {
        bignum ret ;
        bignum_t t ;
        memcpy(ret.num,num,sizeof(bignum_t));
        div(t,ret.num,a.num);
        ret.sgn=(ret.num[0]==1&&!ret.num [1])?0:sgn ;
        return ret ;
    }
    inline int operator%(const int a)
    {
        bignum ret ;
        int t ;
        memcpy(ret.num,num,sizeof(bignum_t));
        div(ret.num,ABS(a),t);
        memset(ret.num,0,sizeof(bignum_t));
        ret.num[0]=1 ;
        add(ret.num,t);
        return t ;
    }
    inline bignum&operator++()
    {
        *this+=1 ;
        return*this ;
    }
    inline bignum&operator--()
    {
        *this-=1 ;
        return*this ;
    }
    ;
    inline int operator>(const bignum&a)
    {
        return sgn>0?(a.sgn>0?comp(num,a.num)>0:1):(sgn<0?(a.sgn<0?comp(num,a.num)<0:0):a.sgn<0);
    }
    inline int operator>(const int a)
    {
        return sgn>0?(a>0?comp(num,a)>0:1):(sgn<0?(a<0?comp(num,-a)<0:0):a<0);
    }
    inline int operator>=(const bignum&a)
    {
        return sgn>0?(a.sgn>0?comp(num,a.num)>=0:1):(sgn<0?(a.sgn<0?comp(num,a.num)<=0:0):a.sgn<=0);
    }
    inline int operator>=(const int a)
    {
        return sgn>0?(a>0?comp(num,a)>=0:1):(sgn<0?(a<0?comp(num,-a)<=0:0):a<=0);
    }
    inline int operator<(const bignum&a)
    {
        return sgn<0?(a.sgn<0?comp(num,a.num)>0:1):(sgn>0?(a.sgn>0?comp(num,a.num)<0:0):a.sgn>0);
    }
    inline int operator<(const int a)
    {
        return sgn<0?(a<0?comp(num,-a)>0:1):(sgn>0?(a>0?comp(num,a)<0:0):a>0);
    }
    inline int operator<=(const bignum&a)
    {
        return sgn<0?(a.sgn<0?comp(num,a.num)>=0:1):(sgn>0?(a.sgn>0?comp(num,a.num)<=0:0):a.sgn>=0);
    }
    inline int operator<=(const int a)
    {
        return sgn<0?(a<0?comp(num,-a)>=0:1):
        (sgn>0?(a>0?comp(num,a)<=0:0):a>=0);
    }
    inline int operator==(const bignum&a)
    {
        return(sgn==a.sgn)?!comp(num,a.num):0 ;
    }
    inline int operator==(const int a)
    {
        return(sgn*a>=0)?!comp(num,ABS(a)):0 ;
    }
    inline int operator!=(const bignum&a)
    {
        return(sgn==a.sgn)?comp(num,a.num):1 ;
    }
    inline int operator!=(const int a)
    {
        return(sgn*a>=0)?comp(num,ABS(a)):1 ;
    }
    inline int operator[](const int a)
    {
        return digit(num,a);
    }
    friend inline istream&operator>>(istream&is,bignum&a)
    {
        read(a.num,a.sgn,is);
        return  is ;
    }
    friend inline ostream&operator<<(ostream&os,const bignum&a)
    {
        if(a.sgn<0)
            os<<'-' ;
        write(a.num,os);
        return os ;
    }
};
int main()
{
    int  t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        bignum jiecheng,ans;
        jiecheng+=1;
        for(int i=1;i<=n;i++)
        {
            jiecheng*=i;
        }
        for(int i=1;i<=n;i++)
        {
            ans=ans+jiecheng/i;
        }
        cout<<ans<<".0"<<endl;
    }
}











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值