E - Secret Santa(n个信装入n个信封至少有一份装对的概率)

题目:https://vjudge.z180.cn/contest/429153#problem/E

这道题是一个错排公式
错排公式:(全部放错的方案数) D(n) = (n-1) [D(n-2) + D(n-1)]
简化版D(n) = [n!/e+0.5] 取整
解1:这道题可以用这两种方式打表,当n很大的时候,这个概率就趋近于一个数了,打出十几个数就知道答案了
解2:这个题也可以用配对的方式推出来一个公式:https://zhidao.baidu.com/question/630026279534495324.html

解2:

#include <bits/stdc++.h>

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
const int N = 1e6 + 9;
typedef long long ll;

ll jie(int n) {
    ll s = 1;
    for (int i = 2; i <= n; i++) s *= i;
    return s;
}
int main() {
    ll n;
    scanf("%lld", &n);
    double s = 1;
    ll flag = -1;
    for (int i = 1; i <= n; i++) {
        double sum = (ll)flag * 1.0 / jie(i);
        if (abs(sum) < 1e-10) break;
        s += sum;
        flag = -flag;
    }
    printf("%.8lf\n", 1 - s);
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值