Secret Santa (错排)

Christmas comes sooner every year. In fact, in one oft-forgotten corner of the world, gift-giving has already started in the form of a Secret Santa syndicate.

Everybody in the small town of Haircombe is going to put their name into a hat. This hat will be given a hearty shuffle, and then afterwards everybody will take turns once more in taking a name back from the hat.

The name each person receives is the name of the fellow citizen to whom they will send a gift.

Of course, one concern with this strategy is that some unfortunate citizens could wind up giving gifts to themselves. What are the chances that this will happen to any of the citizens of Haircombe?

Input
One line containing the number N (1≤N≤1012), the number of citizens who will take part in Secret Santa.

Output
One line containing one real number; the probability that one or more people wind up giving gifts to themselves.

All output must be accurate to an absolute or relative error of at most 10−6.

Sample Input 1 Sample Output 1
2
0.50000000
Sample Input 2 Sample Output 2
3
0.66666667
Sample Input 3 Sample Output 3
6
0.63194444

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;

int main() {
    LL n, a = 1;
    scanf("%lld", &n);
    if (n > 20)
        printf("0.632121\n");
    else if (n == 1)
        printf("1.000000\n");
    else {
        LL s[20], i;
        s[0] = 0;
        s[1] = 1;
        for (i = 2; i < 20; i++) {
            s[i] = i * (s[i - 1] + s[i - 2]);
        }
        long double ans = s[n - 1];
        for (int i = 1; i <= n; i++) {
            ans /= i;
        }
        printf("%6Lf\n", 1 - ans);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值