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


相关推荐

  • 介绍一下redis_redis sortedset

    介绍一下redis_redis sortedset想要操作redis,就需要与redis建立连接。就像操作MySQL一样,需要首先拿到数据库链接。进而,类似于MySQL的DataSource,ActiveMQ的pool,redis也提供了自己的pool–JedisPool。这些”池”理念是相通的,把你从繁琐的手动获取释放链接解放出来,减少了资源消耗,提高了性能。【1】先看源码源码如下:packageredis.clien…

    2025年9月15日
    6
  • TCP的三次握手与四次挥手理解及面试题(很全面)

    TCP的三次握手与四次挥手理解及面试题(很全面)本文经过借鉴书籍资料、他人博客总结出的知识点,欢迎提问序列号seq:占4个字节,用来标记数据段的顺序,TCP把连接中发送的所有数据字节都编上一个序号,第一个字节的编号由本地随机产生;给字节编上序号后,就给每一个报文段指派一个序号;序列号seq就是这个报文段中的第一个字节的数据编号。确认号ack:占4个字节,期待收到对方下一个报文段的第一个数据字节的序号;序列号表示报文…

    2022年5月5日
    38
  • spi总线协议及spi时序图详解_奔创spi

    spi总线协议及spi时序图详解_奔创spi大家好,我是无际。上个章节我们讲解了spi接口定义,今天我们更加深入讲解下spi协议时序图和spi四种模式的用法。刚开始接触单片机开发时,最怕就是看时序图,对于我来说就是奇怪的知识。特别是SPI和IIC的,以前写程序都直接复制别人程序,功能实现就行了也没去研究过数据传输的时候时序具体是怎么样的。那个时候经验也不足,网上搜的资料说的都太学术化了,也看不懂。后面项目做多了,发现最常用到的通信总线无非就是SPI、IIC、USART、CAN、单口通信。理解也慢慢深刻了,现在去分析时序图也更加

    2022年10月9日
    4
  • Java的final关键字详解建议收藏

    Java中的final关键字非常重要,它可以应用于类、方法以及变量。这篇文章中我将带你看看什么是final关键字?将变量,方法和类声明为final代表了什么?使用final的好处是什么?最后也有一些使

    2021年12月20日
    44
  • 固态硬盘数据丢失能恢复吗?含泪分享:固态硬盘数据恢复方法

    固态硬盘数据丢失能恢复吗?含泪分享:固态硬盘数据恢复方法固态硬盘数据丢失能恢复吗?有些时候又是数据无缘无故丢失导致我们一头雾水的同时又手足无措。其实不论是固态硬盘,还是什么其他电子设备,数据丢失也是可以恢复的。固态硬盘的话,除非芯片被损坏或烧毁。那么点进来看看恢复的方法吧!…

    2026年2月3日
    4
  • 利用正则表达式限制网页表单里的文本框输入内容

    利用正则表达式限制网页表单里的文本框输入内容

    2021年7月28日
    70

发表回复

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

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