hdoj 1099 Lottery(期望)

【题目大意】:给出n张不同的牌,每种无限张,问你收集齐n张不同的牌所需的平均次数。


【解题思路】:简单的期望题,假设现在手中有k张不同的牌,则下一张是不同的牌的概率是 (n-k)/n。那么抽到不重复的牌的期望张数是 n/(n-k)...

                            把n提出来~求E=n*sigema(1/k)(1<=k<=n)

                            一时大意,求分母的时候没边求边约分,爆longlong了wa了一次,没意识啊。


【代码】:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <string>
#include <cctype>
#include <map>
#include <iomanip>
                   
using namespace std;
                   
#define eps 1e-8
#define pi acos(-1.0)
#define inf 1<<30
#define linf 1LL<<60
#define pb push_back
#define lc(x) (x << 1)
#define rc(x) (x << 1 | 1)
#define lowbit(x) (x & (-x))
#define ll long long

int n;

ll get_len(ll n){
    ll len=0;
    while (n){
        n/=10,len++;
    }
    return len;
}

ll GCD(ll a,ll b){
    while (b) {
        ll c;
        c=a%b,a=b,b=c; 
    }
    return a;
}

void solve() {
    ll tmp=1;
    for (int i=1; i<=n; i++) tmp=tmp*i/GCD(tmp,i);
    ll ans=0;
    for (int i=1; i<=n; i++) ans+=tmp/i;
    ans*=n;
    ll gcdtmp;
    gcdtmp=GCD(ans,tmp);
    ans=ans/gcdtmp;
    tmp=tmp/gcdtmp;
    ll k;
    k=ans/tmp;
    ans=ans%tmp;
    if (ans==0) {cout << k << endl;}
    else {
        ll len1,len2;
        len1=get_len(k);
        len2=get_len(tmp);
        for (int i=1; i<=len1+1; i++) cout <<" " ;
        cout << ans << endl;
        cout << k << " ";
        for (int i=1; i<=len2; i++) cout << "-";
        cout << endl;
        for (int i=1; i<=len1+1; i++) cout << " ";
        cout << tmp << endl;
    }
}

int main() {
    while (~scanf("%d",&n)){
        solve();
    }
    return 0;
}


你可能感兴趣的:(hdoj 1099 Lottery(期望))