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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值