UVA - 11637 Garbage Remembering Exam (组合+概率)

Little Tim is now a graduate,and is thinking about higher studies. However, he first needs to appear in anexam whose preparation alone includes memorizing the meanings of over 3500words!

After going through the list afew times, however, he sees trouble – he can remember all the word-meanings aslong as they keep coming in the same order. When quizzed in random order,however, he is at a complete loss. So, as any decent programmer would, hewrites
a random shuffler to help him quiz himself.

To his dismay, however, hefinds that words that were near each other in the original list quite often endup being close together in the shuffled list as well. He does not like this atall, and suspects that his random shuffler may have some kind of a bug.
Butbefore he recodes his shuffler he wants your help to make sure that theshuffler is indeed faulty.

So he is asking for your helpin determining how likely such events are even if the list is indeed gettingcompletely randomly shuffled, and even if his program is working perfectly.

Given the size of the list N,and a proximity factor K, you are to determine the expected number of
wastedwords in a shuffled list assuming that all possible shufflings are equallylikely. Informally, two words are considered
wasted if they appear at adistance less than or equal to K in both the lists. Assume that theoriginal list is
cyclical and shuffled list is linear (non-cyclical).

Formally, let us suppose thattwo words A and B have indices oa and
ob inthe original list and indices sa and
sb inthe shuffled list, respectively (all indices are 0-based). Then both the wordsare considered
wasted if:

and

Input

The input consists of a seriesof cases. Each case consists of two integers
N
and K on a singleline. You may assume that 1≤K≤N≤100000.Input is terminated by a line containing two 0s, and has at most 125 lines ofinput.

Output

Output oneline for each test case except the last one. Each line of output should be ofthe form “Case X: Y”, where X is the serial number of output and Y is  the expected number of wasted words in theshuffled list, printed with exactly four digits after
the decimal point, withrounding if necessary.

SampleInput                             Outputfor Sample Input


5 2

5 1

0 0


Case 1: 5.0000

Case 2: 3.5000

题意:输入n和k,你的任务是计算平均情况下,无效单词的个数,计算方法是:两个单词在重新排列后的位置不超过k

思路:我们先计算有效的位置,枚举后,从剩下的选出2*k来计算,用log来计算

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn = 100005;

int n, k;
long double f[maxn];

void solve() {
	if (n == 1) {
		printf("0.0000\n");
		return;
	}
	if (n <= 2 * k + 1) {
		printf("%d.0000\n", n);
		return;
	}

	int N = k << 1, p;
	long double sum = 0;
	for (int i = 1; i <= n; i++) {
		p = max(i-1-k, 0) + max(n-k-i, 0);
		if (p < N)
			continue;
		sum += exp(f[p] + f[n - N - 1] - f[n - 1] - f[p - N]);
	}
	printf("%.4lf\n", (double)(n - sum));
}

int main() {
	f[0] = f[1] = 0;
	for (int i = 2; i <= maxn; i++)
		f[i] = f[i-1] + log((long double) i); 

	int cas = 1;
	while (scanf("%d%d", &n, &k) != EOF && n) {
		printf("Case %d: ", cas++);
		solve();
	}
	return 0;
}
时间: 2024-08-05 13:53:09

UVA - 11637 Garbage Remembering Exam (组合+概率)的相关文章

uva 11637 - Garbage Remembering Exam(概率)

题目链接:uva 11637 - Garbage Remembering Exam 题目大意:大白数里有很详细的题意. 解题思路:对于有序的序列来说,考虑每个位置为有效性的概率.C(2?kn?1?x)?A(2k2k)?A(n?1?xn?1?x)A(n?1n?1) x为考虑当前位置,然后与该位置距离小于等于k的位置个数.该位置有效的话,则对应的要将原先邻近的2*k个单词放到另外的位置上. #include <cstdio> #include <cstring> #include &l

UVA 11637 - Garbage Remembering Exam(组合概率)

UVA 11637 - Garbage Remembering Exam 题目链接 题意:大概意思是,有n个单词,分别打乱放在一个环形的,一个非环形里面,环形的两个单词距离为顺时针逆时针的最小值,非环形的就是位置的差的绝对值,如果有一对单词,在两个里面的距离都是不大于k,那么这单词为无效单词,问平均会出现多少个无效单词 思路:组合概率,假设在非环形形成了一个随机序列,那么我们给它标号1-n,如果我们能分别算出1-n的有效概率,那么就等于算出了无效概率,那么有效概率等于和它距离大于k的那些位置的所

UVA - 11637 Garbage Remembering Exam (组合+可能性)

Little Tim is now a graduate,and is thinking about higher studies. However, he first needs to appear in anexam whose preparation alone includes memorizing the meanings of over 3500words! After going through the list afew times, however, he sees troub

uva 11927 - Games Are Important(组合游戏+记忆化)

题目链接:uva 11927 - Games Are Important 题目大意:给出一张无环有向图,并给出每个节点上的石子数,每次操作可以选择一个石子,向下一个节点移动.两人轮流操作,直到不能操作为失败者. 解题思路:有了图之后,用记忆化的方式处理出每个节点的SG值,取所有石子数为奇数的节点的Nim和. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; con

Hdu 4465 Candy (快速排列组合+概率)

题目链接: Hdu 4465 Candy 题目描述: 有两个箱子,每个箱子有n颗糖果,抽中第一个箱子的概率为p,抽中另一个箱子的概率为1-p.每次选择一个箱子,有糖果就拿走一颗,没有就换另外一个箱子.问换箱子的时候,另外一个箱子中剩下糖果的期望值. 解题思路: 注意题目描述,其中任意一个箱子没有糖果,另一个箱子中剩下糖果个数的期望,而不是第一个箱子没有糖果.不是把其中一个箱子取空时,另一个箱子剩下糖果的期望,而是其中一个箱子取空再换另外一个箱子时,这个箱子的期望. 可以根据期望性质画出公式:an

UVA 11021 Tribles(递推+概率)

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=33059 [思路] 递推+概率. 设f[i]表示一只Tribble经过i天之后死绝的概率,则有递推式: f[i]=p[0]+p[1]*(f[i-1]^1)+…p[n-1]*(f[i-1]^n-1) 最后答案为f[m]^k [代码] 1 #include<cstdio> 2 #include<cstring> 3 #define FOR(a,b,c) f

UVA 10755 Garbage Heap 最大子立方体和

点击打开链接 10755 - Garbage Heap Time limit: 3.000 seconds 最大子立方体和比最大子矩阵多一维,同样转换为一维,然后求最值. #include<stdio.h> #include<string.h> #include<algorithm> #define ll long long #define inf 1ll<<60//加ll using namespace std; ll sum[27][27][27]; l

UVA 10491 Cows and Cars 数学 概率

题目链接: https://vjudge.net/problem/UVA-10491 题目描述: 有a头牛, b辆车, 在你的一次选择后主持人会为你打开C扇有牛的门并问你换门还是不换门, 输出总是换门情况下可以获得车的概率 解题思路: 我现在还没有写代码......我不知道我的思路对不对, 但是先说一下, 我们可以分情况来讨论, 然后分情况乘上获得换门情况下获得车的概率, 也就是全概率公式, 情况分成两种, 一开始指着车, 和一开始指着牛 , res = a/(a+b) * b/(a+b-c-1

UVA - 11722 Joining with Friend (概率)

You are going from Dhaka to Chittagong by train and you came to know one of your old friends is going from city Chittagong to Sylhet. You also know that both the trains will have a stoppage at junction Akhaura at almost same time. You wanted to see y