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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 我的LaTeX入门

    我的LaTeX入门第一次打美赛就用了latex,比赛前刷了各种博客,学习了1天就上场。美赛期间全程扮演不同角色,就是打杂的,大家都是第一次参加,都很累,不过我是最累的,两天两夜没睡。建模,编程,latex写论文全程参与。用latex写论文真的是太爽了,闲着也是闲着,不如把latex好好学习下,方便以后建模比赛提高速度。总结下其他博主的笔记LaTeX概览摘自维基百科:LaTeX,是一种基于TEX的排版系统,由美国电…

    2022年6月8日
    34
  • 数组转对象,对象转数组对不对_对象数组初始化

    数组转对象,对象转数组对不对_对象数组初始化<scripttype=”text/javascript”>//数组转对象vara=[1,2,3,4]varobj={…a}//{0:1,1:2,2:3,3:4}varobj2={}a.forEach((item,index)=>{obj2[index]=item})//{0:1,1:2,2:3,3:4}functiontoObj(a.

    2022年9月11日
    0
  • pycharm过期时间_pycharm自带python吗

    pycharm过期时间_pycharm自带python吗用cleanmyMac查看。右键在finder中显示可以找到文件位置。image.png/Users/xxx/Library/Saved\Application\State/com.jetbrains.pycharm.savedState/Users/xxx/Library/Preferences/com.jetbrains.pycharm.plistPycharm用30天就过期,文件是在co…

    2022年8月26日
    2
  • python初级:基础知识-函数

    python初级:基础知识-函数

    2021年10月6日
    30
  • 常用数据库排名及分类介绍[通俗易懂]

    常用数据库排名及分类介绍[通俗易懂]DB-Engines:2019年6月全球数据库排行DB-Engines数据库流行度排行榜6月更新已发布,排名前二十如下:总体排名和上个月相比基本一致,其中排名前三的Oracle、MySQL和MicrosoftSQLServer也是分数增加最多的三个数据库,增加的分数分别为13.67、4.67和15.57,三者的总分也均已超过一千。一、数据库的分类…

    2022年5月7日
    45
  • python基础语法个人笔记_python基础常用语句

    python基础语法个人笔记_python基础常用语句python语法规范python的语法规范非常重要,简洁明了是python的特性,以下是python语法的一些说明python3的编码格式是unicode(utf-8)标识符的规则:由字母、数字

    2022年7月30日
    3

发表回复

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

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