组队训练 F. Fairy, the treacherous mailman

F. Fairy, the treacherous mailman

https://codeforces.com/group/5yyKg9gx7m/contest/281416/problem/F
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
inputCopy
3
outputCopy
2

题目描述:

就是相当于把n封信全部送错有多少种。

分析:

这种类型的题有递推公式dp[i]=(n-1)*(dp[i-1]+dp[i-2])。

具体推导参见:https://blog.csdn.net/weixin_30446197/article/details/96823276?utm_medium=distribute.pc_relevant.none-task-blog-baidujs-7

代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<deque>
#include<cmath>
#include<map>
#define sd(x) scanf("%d",&x)
#define ms(x) memset(x,0,sizeof x)
#define fr(i,n) for(int i=1;i<=n;i++)
using namespace std;
typedef long long ll;
const int maxn=3e6+6;
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;
}

 

posted on 2020-05-24 00:12  Aminers  阅读(169)  评论(0编辑  收藏  举报

导航