递推算法题解:错排问题

问题描述

某人写了 n n n封信,并且有 n n n个信封,如果所有的信都装错了信封,那么会有多少种不同的情况?

注意,结果可能很大,需要输出其模 100003 100003 100003的结果。

输入格式

输入一个正整数 n n n

输出格式

输出错排的方案总数。

输入样例

3

输出样例

2

数据范围

1 < = n < = 1 0 5 1<=n<=10^5 1<=n<=105

算法思想(递推)

状态表示

用状态 f ( n ) f(n) f(n)表示 n n n封信的错排方案数。

递推公式

要完成 n n n封信的错排、计算 f ( n ) f(n) f(n)的方案数,可以分成下面两种情况:

  • 将其中 n − 1 n-1 n1封信进行错排,然后从中挑出一封信和第 n n n封信进行交换,即完成 n n n封信的错排。此时方案数为: f ( n − 1 ) × ( n − 1 ) f(n - 1) \times (n - 1) f(n1)×(n1)
  • 将其中 n − 2 n-2 n2封信进行错排,然后把剩下的一封信和第 n n n封信进行交换,也可以完成 n n n封信的错排。此时方案数为: f ( n − 2 ) × ( n − 1 ) f(n - 2) \times (n - 1) f(n2)×(n1)

因此得到递推公式: f ( n ) = ( f ( n − 1 ) + f ( n − 2 ) ) × ( n − 1 ) f(n) = (f(n - 1) + f(n - 2)) \times (n - 1) f(n)=(f(n1)+f(n2))×(n1)

初始状态

  • 1 1 1封信的错排方案数为 0 0 0,即 f ( 1 ) = 0 f(1) = 0 f(1)=0
  • 2 2 2封信的错排方案数为 1 1 1,即 f ( 2 ) = 1 f(2) = 1 f(2)=1

代码实现(cpp)

#include <iostream>
using namespace std;
const int N = 100010, mod = 100003;
int f[N];
int main()
{
    int n;
    cin >> n;
    f[1] = 0, f[2] = 1;
    for(int i = 3; i <= n; i++)
        f[i] = (long long)(f[i - 1] + f[i - 2]) * (i - 1) % mod;
    cout << f[n] << endl;
    return 0;
}

代码实现(python)

n = int(input())
f = {}
f[1] = 0
f[2] = 1
for i in range(3, n + 1):
    f[i] = (f[i - 1] + f[i - 2]) * (i - 1) % 100003
print(f[n])
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你好!对于扫雷游戏的题解,我可以给你一些思路和代码示例。首先,你需要了解扫雷游戏的规则和要求。接下来,你可以使用C++语言来实现游戏逻辑和界面。 下面是一个简单的扫雷游戏的C++代码示例: ```cpp #include <iostream> #include <vector> #include <random> using namespace std; class MinesweeperGame { private: int rows; int cols; vector<vector<char>> board; vector<vector<bool>> revealed; vector<pair<int, int>> directions = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}}; public: MinesweeperGame(int m, int n, int mineCount) { rows = m; cols = n; board.resize(rows, vector<char>(cols, ' ')); revealed.resize(rows, vector<bool>(cols, false)); placeMines(mineCount); calculateNumbers(); } void printBoard() { cout << " "; for (int j = 0; j < cols; j++) { cout << j << " "; } cout << endl; for (int i = 0; i < rows; i++) { cout << i << " |"; for (int j = 0; j < cols; j++) { cout << board[i][j] << "|"; } cout << endl; } } bool isGameOver() { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (board[i][j] == 'M' && revealed[i][j]) { return true; } } } return false; } void reveal(int row, int col) { if (row < 0 || row >= rows || col < 0 || col >= cols || revealed[row][col]) { return; } revealed[row][col] = true; if (board[row][col] == 'M') { return; } if (board[row][col] == '0') { for (auto dir : directions) { reveal(row + dir.first, col + dir.second); } } } private: void placeMines(int mineCount) { random_device rd; mt1

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

少儿编程乔老师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值