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

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

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

 

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:

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

 

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/117532.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • idea 2022 Mac 激活码【中文破解版】

    (idea 2022 Mac 激活码)这是一篇idea技术相关文章,由全栈君为大家提供,主要知识点是关于2021JetBrains全家桶永久激活码的内容IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html4KDDGND3CI-eyJsaWN…

    2022年4月1日
  • php查看版本命令,如何查看php版本?查看php版本的详细步骤分享「建议收藏」

    php查看版本命令,如何查看php版本?查看php版本的详细步骤分享「建议收藏」如何查看php版本?查看php版本的详细步骤分享发布时间:2020-04-1509:55:44来源:亿速云阅读:413作者:小新今天小编给大家分享的是如何查看php版本?查看php版本的详细步骤,很多人都不太了解,今天小编为了让大家更加了解,所以给大家总结了以下内容,一起往下看吧。一定会有所收获的哦。查看php版本的方法1、使用php函数phpinfo():phpinfo函数返回PHP的所有…

    2022年10月24日
  • python开发环境搭建,pycharm安装运行[通俗易懂]

    python开发环境搭建,pycharm安装运行[通俗易懂]一、python安装指南首先我们安装python1、进入官方网站(网址:https://www.python.org/downloads),根据自己的需求选择python的版本,这里我是选择的Python3.7.3,点击DownloadPython3.7.3按钮2、下载完成后点击安装文件包3、双击EXE文件进行安装,如下…

    2022年8月28日
  • 2020-03-28

    2020-03-28电赛准备电赛的每个题目均有硬件设计与软件设计(1)电路特性测试仪输出1kHz正弦波信号,自动测量并显示该放大器的输入电阻。输入电阻测量范围1k0-50k2,相对误差的绝对值不超过10%。硬件要求:电源题、通信高频题(2)电路特性测试仪输出IkHz正弦波信号,自动测量并显示该放大器的输出电阻。输出电阻测量范围500SkQ,相对误差的绝对值不超过软件要求:仪表题、控制题10%。(3)…

    2022年5月13日
  • 【工具使用】SecureCRT的下载、安装图文详细过程介绍[通俗易懂]

    【工具使用】SecureCRT的下载、安装图文详细过程介绍[通俗易懂]本文目录一、SecureCRT的介绍二、SecureCRT的下载三、SecureCRT的安装四、SecureCRT的使用一、SecureCRT的介绍SecureCRT是一款支持SSH(SSH1和SSH2)的终端仿真程序,简单地说是Windows下登录UNIX或Linux服务器主机的软件。SecureCRT支持SSH,同时支持Telnet和rlogin协议。SecureCRT是一款用于连接运行包括Windows、UNIX和VMS的理想工具。通过使用内含的VC

    2022年8月30日
  • CheckListBox的使用方法

    CheckListBox的使用方法checklistbox控件 1.添加项 checkedListBox1.Items.Add(“蓝色”); checkedListBox1.Items.Add(“红色”); checkedListBox1.Items.Add(“黄色”);   2.判断第i项是否选中,选中为true,否则为false if(checkedListBox1.G

    2022年6月26日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号