uva 10288 - Coupons(概率)

题目链接:uva 10288 - Coupons

题目大意:给定n,为有n中兑换卷,现在每开一次箱子,就能等概率的获得其中的一种兑换卷。问说平均情况下需要开多少个箱子才能集齐n种兑换卷。

解题思路:dp[i]表示还有i种没获得,dp[i]=n?in?dp[i]+in?dp[i?1]+1

===》dp[i]=dp[i?1]+ni

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long type;

struct Fraction {
    type member; // 分子;
    type denominator; // 分母;

    Fraction (type member = 0, type denominator = 1);
    void operator = (type x) { this->set(x, 1); }
    Fraction operator * (const Fraction& u);
    Fraction operator / (const Fraction& u);
    Fraction operator + (const Fraction& u);
    Fraction operator - (const Fraction& u);

    void set(type member, type denominator);
};

inline type gcd (type a, type b) {
    return b == 0 ? a : gcd(b, a % b);
}

/*Code*/
/////////////////////////////////////////////////////
const int maxn = 33;
typedef long long ll;

Fraction dp[maxn+5][maxn+5];

void init () {
    for (int i = 1; i <= maxn; i++) {
        dp[i][0] = 0;
        for (int j = 1; j <= i; j++)
            //dp[i][j] = (dp[i][j-1] * Fraction(j, i) + Fraction(1, 1)) / Fraction(j, i);
            dp[i][j] = dp[i][j-1] + Fraction(i, j);
    }
}

inline int countbit (ll u) {
    int ret = 0;
    while (u) {
        ret++;
        u /= 10;
    }
    return ret;
}

void put_ans (Fraction u) {

    type d = u.member / u.denominator;
    type member = u.member % u.denominator;

    int cn = countbit(d);
    int cd = countbit(u.denominator);

    int n = cn + (cn == 0 ? 0 : 1);
    if (member) {
        for (int i = 0; i < n; i++) printf(" ");
        printf("%lld\n", member);
    }

    printf("%lld", d);
    if (member) {
        if (d)
            printf(" ");
        for (int i = 0; i < cd; i++) printf("-");
        printf("\n");

        for (int i = 0; i < n; i++) printf(" ");
        printf("%lld", u.denominator);
    }
    printf("\n");
}

int main () {
    int n;
    init();
    while (scanf("%d", &n) == 1) {
        put_ans(dp[n][n]);
    }
    return 0;
}

/////////////////////////////////////////////////////

Fraction::Fraction (type member, type denominator) {
    this->set(member, denominator);
}

Fraction Fraction::operator * (const Fraction& u) {
    return Fraction(member * u.member, denominator * u.denominator);
}

Fraction Fraction::operator / (const Fraction& u) {
    return Fraction(member * u.denominator, denominator * u.member);
}

Fraction Fraction::operator + (const Fraction& u) {
    return Fraction(member * u.denominator + denominator * u.member, denominator * u.denominator);
}

Fraction Fraction::operator - (const Fraction& u) {
    return Fraction(member * u.denominator - denominator * u.member, denominator * u.denominator);
}

void Fraction::set (type member, type denominator) {
    type tmp_d = gcd(member, denominator);
    this->member = member / tmp_d;
    this->denominator = denominator / tmp_d;
}

uva 10288 - Coupons(概率)

时间: 2024-10-26 18:45:40

uva 10288 - Coupons(概率)的相关文章

UVA 10288 - Coupons(概率递推)

UVA 10288 - Coupons 题目链接 题意:n个张票,每张票取到概率等价,问连续取一定次数后,拥有所有的票的期望 思路:递推,f[i]表示还差i张票的时候期望,那么递推式为 f(i)=f(i)?(n?i)/n+f(i?1)?i/n+1 化简后递推即可,输出要输出分数比较麻烦 代码: #include <cstdio> #include <cstring> #include <cmath> long long gcd(long long a, long lon

UVA - 10288 Coupons (概率+递推)

Description Problem F Coupons Input: standard input Output: standard output Time Limit: 2 seconds Memory Limit: 32 MB Coupons in cereal boxes are numbered 1 to n, and a set of one of each is required for a prize (a cereal box, of course). With one co

UVA 10288 Coupons (概率)

题意:有n种纸片无限张,随机抽取,问平均情况下抽多少张可以保证抽中所有类型的纸片 题解:假设自己手上有k张,抽中已经抽过的概率为 s=k/n:那抽中下一张没被抽过的纸片概率为 (再抽一张中,两张中,三张中...)(1-s)*(1+2*s+3*s^3+...)=(1-s)*E   s*E = (s+2*s^2+3*s^3+...):则E-s*E = (1+s+s^2+s^3+...)(等比数列,且公比不可能为1)=1/(1-s) = n/(n-k)  所以总概率就是n*(1/n+1/(n-1)+.

UVA 10288 Coupons

#include <iostream> #include <stdio.h> #include <cstring> #define N 34 typedef long long LL; using namespace std; struct T{ LL zs,fz,fm; T() { zs = 0; fz = 0; fm = 1; } T(LL a, LL b, LL c) { zs = a; fz = b; fm = c; } void Add(struct T t)

10288 - Coupons(赠券收集问题)(概率)

roblem F Coupons Input: standard input Output: standard output Time Limit: 2 seconds Memory Limit: 32 MB Coupons in cereal boxes are numbered 1 to n, and a set of one of each is required for a prize (a cereal box, of course). With one coupon per box,

UVA 11021 - Tribles(概率递推)

UVA 11021 - Tribles 题目链接 题意:k个毛球,每个毛球死后会产生i个毛球的概率为pi,问m天后,所有毛球都死亡的概率 思路:f[i]为一个毛球第i天死亡的概率,那么 f(i)=p0+p1f(i?1)+p2f(i?1)2+...+pnf(i?1)n 然后k个毛球利用乘法定理,答案为f(m)k 代码: #include <stdio.h> #include <string.h> #include <math.h> const int N = 1005;

UVA 11291 - Smeech(概率+词法分析)

UVA 11291 - Smeech 题目链接 题意:给定一个表达式形如e=(p,e1,e2) 该表达式的值为 p?(e1+e2)+(1?p)?(e1?e2),求出值 思路:题目是很水,但是处理起来还挺麻烦的,模拟写编译器LEX分析器原理去写了. 代码: #include <cstdio> #include <cstring> const int N = 100005; char str[N]; int now, len, token; double value; void get

UVA 11346 - Probability(概率)

UVA 11346 - Probability 题目链接 题意:给定a,b,s要求在[-a,a]选定x,在[-b,b]选定y,使得(0, 0)和(x, y)组成的矩形面积大于s,求概率 思路:这样其实就是求xy > s的概率,那么画出图形,只要求y = s / x的原函数, y = slnx,带入两点相减就能求出面积,面积比去总面积就是概率 代码: #include <cstdio> #include <cstring> #include <cmath> int

UVA 11971 - Polygon(概率+几何概型)

UVA 11971 - Polygon 题目链接 题意:给一条长为n的线段,要选k个点,分成k + 1段,问这k + 1段能组成k + 1边形的概率 思路:对于n边形而言,n - 1条边的和要大于另外那条边,然后先考虑3边和4边形的情况,根据公式在坐标系中画出来的图,总面积为x,而不满足的面积被分成几块,每块面积为x/2k,然后在观察发现一共是k + 1块,所以符合的面积为x?x?(k+1)/2k,这样一来除以总面积就得到了概率1?(k+1)/2k 代码: #include <cstdio>