Fairy, the treacherous mailman【数学签到题】

题目描述:原题链接

Fairy is a very excentric mailman. Every now and then, he likes to change some correspondences that he is responsible for, swapping than to different addresses, as he enjoys the consequent turmoil. One day, Fairy was in his most inspired self and decided to swap all correspondences from their original addresses. No address should receive its intended correspondence. It will be Fairy’s ultimate masterpiece. Although he is keen to chaos, Fairy is also a very curious person, and he asked himself about how many ways he could swap the correspondences so none of the addresses receive the correct message. As he’s very lazy, he decided to ask your help to fulfill his objectives. You’ll receive an integer number N, that stands for the correspondences amount, to which you should generate another integer S that stands for the amount of ways he can swap the correspondences. Help Fairy in his mischievous plan, or he might change your correspondence as well.

Input
A unique integer N, (1≤N≤20) standing for the amount of correspondences.

Output
A unique integer S, representing the amount of ways Fairy can swap the correspondences. As the possibilities amount can be very big, the answer should be given as S mod 109+7.

Example
input
3
output
2

思路:

以n=4时为例,可以将4放入1,2,3,那么剩下的情况如果是两个对应的信对换就是 a[n-2] 的情况,否则时 a[n-1] 的情况。所以得出递推公式 a[n]=(n-1)*(a[n-1]+a[n-2]) 。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
using namespace std;
typedef long long ll;
const int mod=1e9+7;

ll a[26];
int main()
{
	int n;
	cin>>n;
	a[2]=1,a[3]=2;
	for(ll i=4;i<=22;i++)
	{
		a[i]=(i-1)*(a[i-1]+a[i-2])%mod;
	}
	printf("%lld\n",a[n]);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值