标签:des style blog http color os io ar strong
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 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.
2
5
17
3
5
11 --
12
340463
58 ------
720720 题意:收集n个不同的优惠券的期望开箱次数 思路:设f[i]代表还有i个优惠券没有收集到的期望开箱次数,那么我们只要计算再没有取到的期望+上一个取到的期望+操作次数1,即f[i]=(n-i)/n*f[i]+i/n*f[i+1]+1,移项后是 f[i] = ((n-i)/n*f[i+1]+1)/((n-i)/n),然后就是一个分数计算的模板了#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const int maxn = 50; struct fraction { long long numerator; // 分子 long long denominator; // 分母 fraction() { numerator = 0; denominator = 1; } fraction(long long num) { numerator = num; denominator = 1; } fraction(long long a, long long b) { numerator = a; denominator = b; this->reduction(); } void operator = (const long long num) { numerator = num; denominator = 1; this->reduction(); } void operator = (const fraction &b) { numerator = b.numerator; denominator = b.denominator; this->reduction(); } fraction operator + (const fraction &b) const { long long gcdnum = __gcd(denominator, b.denominator); fraction tmp = fraction(numerator*(b.denominator/gcdnum) + b.numerator*(denominator/gcdnum), denominator/gcdnum*b.denominator); tmp.reduction(); return tmp; } fraction operator + (const int &b) const { return ((*this) + fraction(b)); } fraction operator - (const fraction &b) const { return ((*this) + fraction(-b.numerator, b.denominator)); } fraction operator - (const int &b) const { return ((*this) - fraction(b)); } fraction operator * (const fraction &b) const { fraction tmp = fraction(numerator*b.numerator, denominator * b.denominator); tmp.reduction(); return tmp; } fraction operator * (const int &b) const { return ((*this) * fraction(b)); } fraction operator / (const fraction &b) const { return ((*this) * fraction(b.denominator, b.numerator)); } void reduction() { if (numerator == 0) { denominator = 1; return; } long long gcdnum = __gcd(numerator, denominator); numerator /= gcdnum; denominator /= gcdnum; } void print() { if (denominator == 1) printf("%lld\n", numerator); else { long long num = numerator/denominator; long long tmp = num; int len = 0; while (tmp) { len++; tmp/=10; } for (int i = 0; i < len; i++) printf(" "); if (len != 0) printf(" "); printf("%lld\n",numerator%denominator); if (num != 0) printf("%lld ", num); tmp = denominator; while (tmp) { printf("-"); tmp/=10; } puts(""); for (int i = 0; i < len; i++) printf(" "); if (len != 0) printf(" "); printf("%lld\n",denominator); } } } f[maxn]; int main() { int n; while (scanf("%d", &n) != EOF) { f[n] = 0; for (int i = n-1; i >= 0; i--) f[i] = (fraction(n-i, n) * f[i+1] + 1) / fraction(n-i, n); f[0].print(); } return 0; }
标签:des style blog http color os io ar strong
原文地址:http://blog.csdn.net/u011345136/article/details/39058429