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)
上一篇 2022年1月4日 上午11:00
下一篇 2022年1月4日 上午11:00


相关推荐

  • ILRuntime Unity热更新

    ILRuntime Unity热更新在新的项目中 使用到了 ILRuntime 的热更新方式 不同于 XLua 等 这种方式的热更新是由纯 C 实现的 所以就不需要客户端懂 Lua 的代码 更详细的介绍可以看官方的文档 官方的介绍及文档为 http ourpalm github io ILRuntime public v1 guide index html 目前大致的理解为 将游戏分为两个部分 Unity 和 Hotfix 其中主要的游戏逻

    2026年3月26日
    2
  • Linux——iscsiadm基本用法

    Linux——iscsiadm基本用法1 存储介质 1 磁盘阵列 磁盘阵列是一种采用 RAID 技术 冗余技术和在线维护技术制造的一种高性能 高可用的磁盘存储设备 2 IP SAN 存储 SAN StorageAreaN 存储区域网络 是计算机信息处理技术中的一种架构 它将服务器和远程的计算机存储设备 如磁盘阵列 磁带库 连接起来 使得这些存储设备看起来就像是本地一样 SAN 就理解成存储虚拟化 而 IP SAN 就是采

    2026年3月17日
    1
  • tomcat下载安装及配置教程视频_安装tomcat步骤

    tomcat下载安装及配置教程视频_安装tomcat步骤之前选择的版本是tomcat10.0按照下面流程走了一遍,发现一直是未发现的状态。后来,我换成了tomcat9版本就OK了下面以tomcat9.0版本为例讲述其过程

    2025年11月24日
    6
  • Weblogic部署项目三种方式

    Weblogic部署项目三种方式在 weblogic 中部署项目通常有三种方式 第一 在控制台中安装部署 第二 将部署包放在 domain 域中 autodeploy 目录下部署 第三 使用域中配置文件 config xml 进行项目的部署 控制台部署 1 启动 weblogic 服务 登录到 weblogic 控制台页面 输入用户名和密码 登录到控制台里面 2 点击左侧的部署 3 在右侧点击安装按钮 准备

    2026年3月20日
    2
  • 2025终极实测!字节开源Coze保姆级教程,零代码搭建Agent应用,收藏这篇就够了!

    2025终极实测!字节开源Coze保姆级教程,零代码搭建Agent应用,收藏这篇就够了!

    2026年3月13日
    2
  • 知识库 平台_平台开发

    知识库 平台_平台开发入园这么些天了,今天搭建了一套知识库系统,使用效果还不错,分享一些过程经验。搭建准备:软件系统:WCP4.3免费版(免费开源,支持Windows,使用简单,有傻瓜式一键安装包-win平台)服务

    2022年8月4日
    7

发表回复

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

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