The 2020 ICPC Asia Macau Regional Contest L - Random Permutation(概率期望/思维)
An integer sequence with length 𝑛n, denoted by 𝑎1,𝑎2,⋯,𝑎𝑛a1,a2,⋯,an, is generated randomly, and the probability of being 1,2,⋯,𝑛1,2,⋯,n are all 1𝑛1n for each 𝑎𝑖ai (𝑖=1,2,⋯,𝑛)(i=1,2,⋯,n).
Your task is to calculate the expected number of permutations 𝑝1,𝑝2,⋯,𝑝𝑛p1,p2,⋯,pn from 11 to 𝑛nsuch that 𝑝𝑖≤𝑎𝑖pi≤ai holds for each 𝑖=1,2,⋯,𝑛i=1,2,⋯,n.
Input
The only line contains an integer 𝑛n (1≤𝑛≤50)(1≤n≤50).
Output
Output the expected number of permutations satisfying the condition. Your answer is acceptable if its absolute or relative error does not exceed 10−910−9.
Formally speaking, suppose that your output is 𝑥x and the jury's answer is 𝑦y. Your output is accepted if and only if |𝑥−𝑦|max(1,|𝑦|)≤10−9|x−y|max(1,|y|)≤10−9.
Examples
input
Copy
2
output
Copy
1.000000000000
input
Copy
3
output
Copy
1.333333333333
input
Copy
50
output
Copy
104147662762941310907813025277584020848013430.758061352192
首先易知可能的a序列总共有个。p序列一共有种可能(因为是1~n排列),对于每种排列,满足条件的a序列有个,因为对于,a序列只能这样选:,且p1到pn是一个1到n的排列。注意因为是求期望,所以不用考虑重复的情况。
import math
n = eval(input())
print((math.factorial(n)) ** 2 / n ** n)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!