uva10288 Coupons 【概率 分数】
题目:

题意:
一共n种不同的礼券,每次得到每种礼券的概率相同。求期望多少次可以得到所有n种礼券。结果以带分数形式输出。1<= n <=33.
思路:
假设当前已经得到k种,获得新的一种的概率是(n-k)/n,则对应期望是n/(n-k)。求和得到步数期望是n/n+n/(n-1)+...+n/1=n*sum(1/i) (1<= i <= n)。需要注意及时约分,用分数类模板。
程序:
#include <cstdio>
#include <cassert>
#include <cstdlib> long long gcd (long long a, long long b) {
return b == ? a : gcd(b, a % b);
} class fraction {
public:
fraction() {
numerator = ;
denominator = ;
}
fraction(long long num) {
numerator = num;
denominator = ;
}
fraction (long long a, long long b) {
assert(b != );
if (b < ) {
numerator = -a;
denominator = -b;
} else {
numerator = a;
denominator = b;
}
this->reduction();
} void operator = (long long num) {
numerator = num;
denominator = ;
} 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);
return fraction(numerator*(b.denominator/gcdnum) + b.numerator*(denominator/gcdnum), denominator/gcdnum*b.denominator);
} 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 {
return fraction(numerator*b.numerator, denominator * b.denominator);
} 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 == ) {
denominator = ;
return;
}
long long gcdnum = gcd(abs(numerator), denominator);
numerator /= gcdnum;
denominator /= gcdnum;
} public:
long long numerator;//分子
long long denominator;//分母
}; int countLen (long long n) {
int m = ;
while (n) {
n /= ;
m++;
}
return m;
} void printF (const fraction &f){
long long a, b, c;
int m, n;
b = f.numerator;
c = f.denominator;
if (c == (long long)) {
printf("%lld\n", b);
} else {
a = b / c;
m = countLen(a);
b -= a * c;
n = countLen(c);
for (int i = ; i <= m; i++) {
printf(" ");
}
printf("%lld\n%lld ", b, a);
for (int i = ; i < n; i++) {
printf("-");
}
puts("");
for (int i = ; i <= m; i++) {
printf(" ");
}
printf("%lld\n", c);
}
} int main() {
int n, maxn = ;
fraction f[maxn + ]; for (int i = ; i <= maxn; i++) {
f[i] = f[i - ] + fraction(, i);
} while (~scanf("%d", &n)) {
printF(f[n] * n);
} return ;
}
uva10288 Coupons 【概率 分数】的更多相关文章
- UVa10288 Coupons 分数模版
UVa10288 题目非常简单, 答案就是 n/n+n/(n-1)+...+n/1; 要求分数输出.套用分数模板.. #include <cstdio> #include <cstr ...
- UVA 10288 - Coupons(概率递推)
UVA 10288 - Coupons option=com_onlinejudge&Itemid=8&page=show_problem&category=482&p ...
- [POJ2625][UVA10288]Coupons
Description Coupons in cereal boxes are numbered 1 to n, and a set of one of each is required for a ...
- 「SHOI2002」「LuoguP1291」百事世界杯之旅(UVA10288 Coupons)(期望,输出
题目描述 “……在2002年6月之前购买的百事任何饮料的瓶盖上都会有一个百事球星的名字.只要凑齐所有百事球星的名字,就可参加百事世界杯之旅的抽奖活动,获得球星背包,随声听,更克赴日韩观看世界杯.还不赶 ...
- uva 10288 Coupons (分数模板)
https://vjudge.net/problem/UVA-10288 大街上到处在卖彩票,一元钱一张.购买撕开它上面的锡箔,你会看到一个漂亮的图案. 图案有n种,如果你收集到所有n(n≤33)种彩 ...
- ID3、C4.5、CART、RandomForest的原理
决策树意义: 分类决策树模型是表示基于特征对实例进行分类的树形结构.决策树可以转换为一个if_then规则的集合,也可以看作是定义在特征空间划分上的类的条件概率分布. 它着眼于从一组无次序.无规则的样 ...
- Logistic Regression vs Decision Trees vs SVM: Part II
This is the 2nd part of the series. Read the first part here: Logistic Regression Vs Decision Trees ...
- [ An Ac a Day ^_^ ] hdu 4565 数学推导+矩阵快速幂
从今天开始就有各站网络赛了 今天是ccpc全国赛的网络赛 希望一切顺利 可以去一次吉大 希望还能去一次大连 题意: 很明确是让你求Sn=[a+sqrt(b)^n]%m 思路: 一开始以为是水题 暴力了 ...
- 学习笔记TF052:卷积网络,神经网络发展,AlexNet的TensorFlow实现
卷积神经网络(convolutional neural network,CNN),权值共享(weight sharing)网络结构降低模型复杂度,减少权值数量,是语音分析.图像识别热点.无须人工特征提 ...
随机推荐
- Web 端屏幕适配方案
基础知识 像素相关 1.像素 :像素是屏幕显示最小的单位. 2.设备像素 :设备像素又称物理像素(physical pixel),设备能控制显示的最小单位,我们可以把这些像素看作成显示器上一个个的点. ...
- 关于VC中LineDDA函数的调用
在项目里碰到这个函数,不知道怎么使用,记录在这里. 该函数的原型如下: BOOL LineDDA(int nXStart, int nYStart, int nXEnd, int nYEnd, LIN ...
- 接口测试Fiddler实战20150921
(写好的文章被不小心删掉了,现在补一篇) 项目背景: 1.接口URL:http://192.168.xx.xx:8080/mserver/rest/ms 2.接口参数:data=xxxxx&k ...
- ES6中箭头函数的作用
我们知道在ES6中,引入了箭头函数,其本质就是等同有ES5中的函数.类似于下面的写法: let test1=() => “abc”; let test2=() => { return “a ...
- ZetCode PyQt4 tutorial work with menus, toolbars, a statusbar, and a main application window
!/usr/bin/python -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial This program create ...
- 在Linux中批量修改字符串的命令
昨天一个朋友忽然问我,在Linux下如何批量修改字符串,当时瞬间懵逼了,完全想不起来....... 今天特意的重温了一下Linux下的一些常用命令,并将这个遗忘的批量修改字符串的命令记录下来(资料来自 ...
- .NET工具软件收集
======================== ILSPY 官网: http://ilspy.net/ .NET Reflector 官网:http:/ ...
- 利用Web Services开发分布式应用
一.引言 在前面文章中分别介绍了MSMQ和.NET Remoting技术,今天继续分享.NET 平台下另一种分布式技术——Web Services 二.Web Services 详细介绍 2.1 We ...
- ES6必知必会 (七)—— Generator 函数
Generator 函数 1.Generator 函数是 ES6 提供的一种异步编程解决方案,语法行为与传统函数完全不同,通常有两个特征: function关键字与函数名之间有一个星号: 函数体内部使 ...
- Python download a image (or a file)
http://stackoverflow.com/questions/13137817/how-to-download-image-using-requests import shutil impor ...