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;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

UVA - 11637 Garbage Remembering Exam (组合+可能性)的更多相关文章

  1. UVA 11637 Garbage Remembering Exam

    #include <iostream> #include <stdio.h> #include <cstring> #include <math.h> ...

  2. uva 11673 Garbage Remembering Exam (概率)

    题目链接: http://vjudge.net/problem/viewProblem.action?id=42000 该过程为随即过程,因此总期望值等于个单词对应的期望值,即它们wasted的概率 ...

  3. 🔺 Garbage Remembering Exam UVA - 11637()

    题目大意:给你N个单词,有两种方法随机排列,一种随机排成一行,另一种随机排成一圈,当两个单词之间的距离在两种排列中都严格小于K时,则这两个单词构成无效单词,问无效单词的期望. 解题思路:首先对于一排单 ...

  4. uva 10755 - Garbage Heap

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  5. UVA 12906 Maximum Score 排列组合

    Maximum Score Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/vie ...

  6. UVa 10755 Garbage Heap (暴力+前缀和)

    题意:有个长方体由A*B*C组成,每个废料都有一个价值,要选一个子长方体,使得价值最大. 析:我们暴力枚举上下左右边界,然后用前缀和来快速得到另一个,然后就能得到长方体,每次维护一个最小值,然后差就是 ...

  7. UVa 11538 Chess Queen (排列组合计数)

    题意:给定一个n*m的棋盘,那么问你放两个皇后相互攻击的方式有多少种. 析:皇后攻击,肯定是行,列和对角线,那么我们可以分别来求,行和列其实都差不多,n*A(m, 2) + m*A(n, 2), 这是 ...

  8. .Uva&LA部分题目代码

    1.LA 5694 Adding New Machine 关键词:数据结构,线段树,扫描线(FIFO) #include <algorithm> #include <cstdio&g ...

  9. LeetCode 216. Combination Sum III (组合的和之三)

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

随机推荐

  1. java模拟浏览器包selenium整合了htmlunit,火狐浏览器,IE浏览器,opare浏览器驱

    //如果网页源码中有些内容是js渲染过来的,那你通过HttpClient直接取肯定取不到,但是这些数据一般都是通过异步请求传过来的(一般都是通过ajax的get或者post方式).那么你可以通过火狐浏 ...

  2. SVNKIT一段代码的分析

    打印SVNkit版本库中的结构: 函数如下: 调用方法如下: listEntries(repository, ""); System.out.println("XXXXX ...

  3. 纯CSS滑动效果

    原文地址:Pure CSS Slide Up and Slide Down 示例地址:Pure CSS Slide Demo 原文日期: 2013年08月26日 翻译日期: 2013年08月27日 如 ...

  4. Sesson禁止使用Cookie

    于WebRoot文件夹MEAT-INF新建文件夹context.xml 增加<context path="XXX" cookies='false"> 更改To ...

  5. Java整型数组的最大长度到底有多长?

    Java整型数组的最大长度到底有多长? 今天上网查了一下,各种说法都有,这个问题似乎总困扰我们Java初学者,无奈,只好自己试了一下,以下是我的测试代码,如果有错误,还望不吝赐教! 使用eclipse ...

  6. boost::signals::signal的使用方法

    吃力的讲完boost::signals的ppt.然后接着就是做练习题. 通过讲ppt,发现有一句话说的真好:你自己知道是一回事.你能给别人讲明确又是另外一回事.真的有些东西你自己理解,可是用语言去非常 ...

  7. swift学习一:介绍,开发文档下载

    在今天wwdc2014公布会上.苹果今天公布了全新的编程语言Swift以及新版Xcode.对于开发人员来说,Swift包括了非常多开发人员喜欢的功能,能够与Objective-C和C语言共同工作.Sw ...

  8. JAVA先进-设置(1)

    >Arrays 基本阵列 1.常见的数组产生于main() 函数,数组下标的索引不能超过0到int的范围 2.当程序试图訪问数组的第一个或者最后一个数据的时候,会发生ArrayIndexOutO ...

  9. slider使用TickPlacement获得游标效果

    <Slider Name="slider游标效果" Maximum="3" SmallChange="0.25" TickPlacem ...

  10. 对HGE游戏引擎的一次封装

    HGE游戏引擎是一个开源2D游戏引擎,基于directX. 它的渲染及逻辑是基于帧回调的框架模式, 其提供一些主要的图像操作和输入控制功能. 我在之前写一个2D游戏的时候对它整个框架进行了一次封装,非 ...