HDU 4986 Little Pony and Alohomora Part I(递推+犹拉常数)

HDU 4986 Little Pony and Alohomora Part I

题目链接

题意:一些钥匙随机放在箱子里,现在问打开次数期望

思路:每种方式相当于一个置换的循环个数,那么考虑f[i]为i个箱子的情况,f[i + 1]要么就是放在最后多一个循环,要么就是插入中间循环个数不变,对应的转移为f[i + 1] = (f[i] + 1) / i + f[i] * (i - 1) / i 化简得到f[i] = f[i - 1] + 1 / i

这个式子i越大,越趋近lni + C,这个C为犹拉常数,所以先递推出数字小的情况,大的就直接计算

代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

const int N = 1000005;
const double sb = 0.577215664901;

int n;

double ans[N];

int main() {
	for (int i = 1; i < N; i++)
		ans[i] = ans[i - 1] + 1.0 / i;
	while (~scanf("%d", &n)) {
		if (n >= N) printf("%.4lf\n", sb + log(n * 1.0));
		else printf("%.4lf\n", ans[n]);
	}
	return 0;
}
时间: 2024-10-12 10:49:24

HDU 4986 Little Pony and Alohomora Part I(递推+犹拉常数)的相关文章

hdu 1165 Eddy&#39;s research II(数学题,递推)

// Eddy 继续 Problem Description As is known, Ackermann function plays an important role in the sphere of theoretical computer science. However, in the other hand, the dramatic fast increasing pace of the function caused the value of Ackermann function

HDU - 2604 Queuing(矩阵快速幂或直接递推)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2604 题意:给出字符串长度L,并且字符串只由'f','m'构成,有2^L种情况,问在其中不包含'fmf','fff'的字符串有多少个. 1.直接递推,虽然过了,但是数据稍微大点就很可能TLE,因为代码上交上去耗时还是比较长的(感觉数据有点水)╭(′▽`)╭(′▽`)╯( 1 #include <iostream> 2 #include <cstdio> 3 #include <c

HDU 5950 - Recursive sequence - [矩阵快速幂加速递推][2016ACM/ICPC亚洲区沈阳站 Problem C]

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 Farmer John likes to play mathematics games with his N cows. Recently, they are attracted by recursive sequences. In each turn, the cows would stand in a line, while John writes two positive numbers

【HDOJ】4986 Little Pony and Alohomora Part I

递推.设n个盒子的Spell次数为S(n),期望为E(n).当有n个盒子时,可能第n把钥匙在第n个盒子中,此时的Spell次数应该为(n-1)!+S(n-1):当第n把钥匙不在第n个盒子中,混合排列,此时的Spell次数为(n-1)*S(n-1),因此,期望E(n) = S(n)/n!,S(n) = (n-1)!+S(n-1) + (n-1)*S(n-1) = (n-1)!+n*S(n-1),则E(n) = S(n-1)/(n-1)! + 1/n = E(n-1) + 1/n.因此,得到递推公式

hdu 5273 Dylans loves sequence 逆序数简单递推

Dylans loves sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5273 Description Dylans得到了N个数a[1]...a[N].有Q个问题,每个问题形如(L,R)他需要求出L−R这些数中的逆序对个数.更加正式地,他需要求出二元组(x,y)的个数,使得L≤x,y≤R且x<y且a[x]>a[y] Input 第一行有两个数N和Q

HDU 4291 A Short problem 短问题 (递推,微变型)

题意:给出递推式 g(n) = 3g(n - 1) + g(n - 2),且g(1) = 1,g(0) = 0.求g( g( g(n))) mod 109 + 7. 思路:要求的g( g( g(n)))一共里外3层.看到时间限制1s,数据最大10^18,必定不能老实递推,要么有循环,要么用通项公式.这里用通项公式太麻烦了,数字不好算,而g(n)%109 + 7是有规律的, 在n=222222224之后会出现循环,也就是n=0和n=222222224的g(n)是一样的,这是最外层.那么也就是说在g

hdu 2190 重建希望小学(数学,递推)

题意: N*3的教室,有2种砖,2*2.1*1. 问铺设教室的方案有多少种.(要铺满) 思路: 画一下图可以很快发现递推公式 代码: int main(){ int a[35]; mem(a,0); a[1]=1, a[2] = 3; rep(i,3,30){ a[i] = a[i-1]+a[i-2]*2; } int T; cin>>T; while(T--){ int n; cin>>n; cout<<a[n]<<endl; } return 0; }

hdu 1292 &quot;下沙野骆驼&quot;ACM夏令营 (递推)

"下沙野骆驼"ACM夏令营 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 833    Accepted Submission(s): 403 Problem Description 大家都知道,杭电计算机学院为了吸引更多的学生参与到程序设计竞赛中去,从2005年秋天,开始举行月赛,并一直坚持到了现在.事实表明,这项措施的效

HDU 4686 Arc of Dream(矩阵加速递推)

题目大意:就是给你你个有两个递推公式乘起来的式子,让你求出第n项的结果. 注意这种递推的需要把式子乘起来然后再构造矩阵. Arc of Dream Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 2092    Accepted Submission(s): 664 Problem Description An Arc of Dream