HDU 1465 不容易系列之一 (错排)

【题目链接】
http://poj.org/problem?id=1465

题目意思

给定n个封信,问n封信都装错有多少种错误方式。

解题思路

错排公式:F(n)=(n-1)[F(n-1)+F(n-2)]
解释:假定A,B,C,D为已经排列的。当E加入时候就会出现两种情况让排列为错排。
1. A,B,C,D已经为错排,那么只要将E与其中任意一个(4种选择)互换就可以使排列为错排。A,B,C,D错排为F(4)。
2. A,B,C,D中有一个正确,其他3个位错误。那么只要将正确的与E互换排列将为错排。而谁为正确有4种选择。3个错排为F(3).
综合推广就得到错排公式。

代码部分


#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <string>
#include <map>
#include <math.h>
using namespace std;
#define LL long long
#define inf 0x3f3f3f3
const int N = 1e3+7;
LL a[25]; 
int main()
{
    a[2] = 1,a[3] = 2;
    for (int i = 4; i < 25; i++)
        a[i] = a[i-1]*(i-1)+a[i-2]*(i-1);
    int n;
    while (~scanf("%d",&n))
    {
        printf("%lld\n",a[n]);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值