列表

详情


NC236485. Glass Bead Game

描述

"Abstractions are fine, but I think people also have to breathe air and eat bread.'' is a quote from Das Glasperlenspiel, or The Glass Bead Game, a novel published in 1946 written by the German author Hermann Hesse.

You are playing with n distinct glass beads in some order. In each step, you move exactly one bead to the front. The cost of moving the bead to the front is the number of beads before that bead. For example, if we move in the list the total cost is 3 and the resulting sequence of beads is .

Suppose that at each step glass bead is moved with probability , where . What is the limit of the expected cost of the m-th move, when m tends to infinity?

输入描述

The first line contains an integer n ().

The second line contains n real numbers each with 6 digits of precision.

输出描述

Output one real number, the answer.

Your answer will be considered correct if its absolute or relative error does not exceed . Formally let your answer be a, jury answer be b. Your answer will be considered correct if .

示例1

输入:

2
0.500000 0.500000

输出:

0.500000000000000

示例2

输入:

3
0.500000 0.250000 0.250000

输出:

0.916666666666667

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++(clang++ 11.0.1) 解法, 执行用时: 3ms, 内存消耗: 468K, 提交时间: 2023-05-03 14:25:09

#include <bits/stdc++.h>
using namespace std;
#define N 105
int n;
double p[N],g;
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++) cin>>p[i];
    for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) g+=(p[i]*p[j])/(p[i]+p[j]);
    printf("%.9lf\n",g*2);
}

pypy3 解法, 执行用时: 128ms, 内存消耗: 26976K, 提交时间: 2022-04-18 10:09:57

n = int(input())
ans = 0
p = list(map(float, input().split()))
ans = 0
for i in range(n):
    for j in range(n):
        if i != j:
            ans += p[i]*p[j]/(p[i]+p[j])

print(ans)

Python3 解法, 执行用时: 44ms, 内存消耗: 4544K, 提交时间: 2022-04-21 10:38:34

n=input()
p=list(map(float,input().split()))
ans=0
for i,pi in enumerate(p):
    for j,pj in enumerate(p):
        if i!=j:
            ans+=pi*pj/(pi+pj)
print(ans)

上一题