uva 10288 gailv

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 coupon per box, how many boxes on average are required to make a complete set ofn coupons?

Input

Input consists of a sequence of lines each containing a single positive integern, 1<=n<=33, giving the size of the set of coupons. Input is terminated by end of file.

Output

For each input line, output the average number of boxes required to collect the complete set ofn coupons. If the answer is an integer number, output the number. If the answer is not integer, then output the integer part of the answer followed by a space and then by the proper fraction in the format shown below. The fractional part should be irreducible. There should be no trailing spaces in any line of output.

Sample Input

2
5
17
Sample Output

3
   5
11 --
   12
   340463
58 ------

记得每次求gcd不然会爆掉。 思路:到k张时。我们还有n-k张没有得到。所以我们得到的概率为n-k/n;

所以到k+1步的期望时n/n-k;

整理得 n* 1/i(n>i>1)的累加

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
#define ll long long
ll gcd(ll n,ll m)
{
    if(m==0)
        return n;
    return gcd(m,n%m);
}
int geth(ll x)
{
    int ans=0;
    while(x>0)
    {
        x=x/10;
        ans++;
    }
    return ans;
}
int main()
{
    ll  n;
    while(~scanf("%lld",&n))
    {

        ll fenmu=1;
        ll temp=1;
        for(int i=1;i<=n;i++)
        {
            temp=gcd(fenmu,i);
            fenmu=fenmu*i/temp;
        }
        ll fenzi=0;
        for(int i=1;i<=n;i++)
        {
            fenzi+=fenmu/i;

        }
        fenzi=fenzi*n;
        ll p=gcd(fenzi,fenmu);
        fenzi/=p;
        fenmu/=p;
        ll t=fenzi/fenmu;
        if(fenzi-fenmu*t>0)
        {
            if(t!=0)
            {
                ll a=geth(t);
                for(int i=0;i<=a;i++)
                {cout<<" ";}
                cout<<fenzi-fenmu*t;
                ll b=geth(fenzi-fenmu*t);
                ll c=geth(fenmu);
                if(b>c)
                    c=b;
                cout<<endl;
                cout<<t<<" ";
                for(int i=1;i<=c;i++)
                {cout<<"-";}
                cout<<endl;
                for(int i=0;i<=a;i++)
                {cout<<" ";}
                cout<<fenmu<<endl;
            }
        }
        else
            cout<<t<<endl;

    }

    return 0;
}

原文地址:https://www.cnblogs.com/2014slx/p/9538251.html

时间: 2024-11-22 16:57:13

uva 10288 gailv的相关文章

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(概率)

题目链接: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 names

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)

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

题意: 每张彩票上印有一张图案,要集齐n个不同的图案才能获奖.输入n,求要获奖购买彩票张数的期望(假设获得每个图案的概率相同). 分析: 假设现在已经有k种图案,令s = k/n,得到一个新图案需要t次的概率为:st-1(1-s): 因此,得到一个新图案的期望为(1-s)(1 + 2s + 3s2 + 4s3 +...) 下面求上式中的级数: 令 则 所以得到一个新图案的期望为: 总的期望为: 这道题的输出很新颖,如果是分数的话,就要以分数形式输出,具体细节详见代码. 1 #include <i

UVa 10288 Coupous (条件概率)

题目 题目大意 大街上到处在卖彩票, 一元钱一张.购买撕开它上面的锡箔, 你会看到一个漂亮的图案.图案有\(n\)种, 如果你收集到所有\(n\)(\(n ≤ 33\))种彩票, 就可以得到大奖.请问, 在平均情况下, 需要买多少张彩票才能得到大奖呢? 如\(n = 5\)时的答案为\(11\frac{5}{12}\). 题解 设已经有\(k\)个图案: 平均拿\(\frac{n}{n - k}\)次就可多搜集一个, 所以总次数为: \[n\sum_{i = 0}^{n - 1}\frac{1}

优质题表(机密版)

转载请注明出处:http://www.cnblogs.com/dashuzhilin/p/4556803.html 思维题: poj 1528 poj 1597 poj 2538 poj 2608 poj 2612 poj 2361 poj 2339 poj 2664 uva 10894 uva 10921   uva 10922   uva 10929 uva 10931   uva 10800   uva 10878 uva 10976   uva 10323   uva 201 poj 2

数学第二课

几个符号的认识: 或      |   ,   c=a|b,二进制下c的第i位为1,当且当a和b的第i位不同时为0,即至少有一个1. 异或  ^   ,   c=a^b,二进制下c的第i位为1,当且当a和b的第i位不相同. 且     &   ,   c=a|b,二进制下c的第i位为1,当且当a和b的第i位同时为1. 性质: ‘或’具有扩散性,即一个数列,‘或’的个数越多,结果越大. ‘且’具有收敛性,个数越多,结果越小. 例题: Interview ,CodeForces - 631A 题意: