Favorite Dice(期望dp)

本文探讨了一个神奇的N面骰子,通过概率论和动态规划的方法,计算了要使每个面至少出现一次所需的平均投掷次数。文章提供了一种逆向DP的解题思路,并附带了C++代码实现。

BuggyD loves to carry his favorite die around. Perhaps you wonder why it's his favorite? Well, his die is magical and can be transformed into an N-sided unbiased die with the push of a button. Now BuggyD wants to learn more about his die, so he raises a question:

What is the expected number of throws of his die while it has N sides so that each number is rolled at least once?

Input

The first line of the input contains an integer t, the number of test cases. t test cases follow.

Each test case consists of a single line containing a single integer N (1 <= N <= 1000) - the number of sides on BuggyD's die.

Output

For each test case, print one line containing the expected number of times BuggyD needs to throw his N-sided die so that each number appears at least once. The expected number must be accurate to 2 decimal digits.

Example

Input:
2
1
12

Output:
1.00
37.24

题目大概:

给出一个n个面的筛子,问要使得每个面都朝上一次,需要投掷多少次。

思路:

期望dp一般逆推。

一般都是dp[i]表示已经出了i个面,距离n个面还差 的期望。

第i次投掷可以落在前i个面,概率为i/n,也可以落在另外的(n-i)个面,概率是(n-i)/n。

每次投掷都百分百的增加投掷次数1.

dp[i]=i/n*dp[i]+(n-i)/n*dp[i+1]+1.

化简的dp[i]=dp[i+1]+n/(n-i).

代码:

#include <bits/stdc++.h>

using namespace std;

double dp[1100];

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(dp,0,sizeof(dp));
        double n;
        scanf("%lf",&n);
        dp[(int)n]=0;
        for(int i=n-1;i>=0;i--)
        {
            dp[i]=dp[i+1]+n/(n-(double)i);
        }
        printf("%lf\n",dp[0]);
    }
    return 0;
}

 

### Soft Dice Loss Soft Dice Loss 是一种用于处理多分类分割问题的损失函数。该方法针对每个类别分别计算Dice系数并取平均作为最终的结果[^1]。 ```python import torch.nn.functional as F def soft_dice_loss(input, target): smooth = 1. input = F.softmax(input, dim=1) intersection = (input * target).sum() union = (input + target).sum() dice = (2. * intersection + smooth) / (union + smooth) return 1 - dice.mean() ``` 此损失函数特别适用于医学图像分析等领域,在这些场景下,不同类别的像素数量可能存在显著差异,而传统的交叉熵损失可能会偏向于占主导地位的大面积区域。 ### 使用 MedPy 库计算 Dice 相似系数 对于具体的二元分割任务,可以通过Python库`medpy`来快速获得两个集合之间的Dice相似系数[^2]: ```python from medpy.metric.binary import dc import numpy as np predict = np.random.randint(0, 4, size=(2, 3)) ground_truth = np.random.randint(0, 4, size=(2, 3)) dice_similarity_coefficient = dc(predict, ground_truth) print(f"Dice Similarity Coefficient is {dice_similarity_coefficient}") ``` 上述代码片段展示了如何定义预测结果与实际标签,并通过调用 `dc()` 函数获取两者间的Dice相似度得分。 ### DICE 模型概述 值得注意的是,“DICE”也指代另一种完全不同的概念——即基于因果推断技术构建的一种推荐系统模型。这种情况下,DICE旨在缓解因物品受欢迎程度而导致的偏差现象,而不是用来衡量重叠程度或者作为优化目标的一部分[^3]。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值