版权声明:蒟蒻写的文章,能看就行了,同时欢迎大佬们指点错误 https://blog.csdn.net/Algor_pro_king_John/article/details/89784749
Problem
有一个 的矩阵,固定第一个数为 ,其他填正整数, 且相邻数的差不能超过 ,求方案数。
Solution
容易发现答案是 。
其中 表示从 走到 可以向上,向下向右走一格,但是只能在第一象限的方案数。
然后这个显然可以用 组合数 + 卡特兰数 推一波: 但时间复杂度是 的。
然后去学了一发姿势,发现这个是所谓的默慈金数:
一个给定的数 的默慈金数是:
- 在一个圆上的 个点间,画出彼此不相交的弦的方案数
其中,
可以推导出
有较好英文水平姿势的同学可以参考推导极其生成函数(反正我是不可能会的),考场上我觉得只要会 的方法,然后只需知道它是由 推到 ,找一下规律应该可以。。。
http://mathworld.wolfram.com/MotzkinNumber.html
http://www.docin.com/p1-964777006.html
Code
#include <bits/stdc++.h>
#define F(i,a,b) for (int i = a; i <= b; i ++)
using namespace std;
const int N = 1e6 + 10;
const int Mo = 1e9 + 7;
long long f[N], M[N], n;
int ksm(int x, int y) {
int ans = 1;
for (; y ; y >>= 1, x = (1ll * x * x) % Mo)
if (y & 1)
ans = (1ll * ans * x) % Mo;
return ans;
}
int main() {
scanf("%d", &n);
f[1] = 1, f[2] = 2;
M[1] = 1, M[2] = 2;
F(i, 3, n) {
M[i] = ((2 * i + 1) * M[i - 1] + (3 * i - 3) * M[i - 2]) % Mo * ksm(i + 2, Mo - 2) % Mo;
f[i] = (f[i - 1] * 3 - M[i - 2]) % Mo;
}
printf("%d\n", (f[n] + Mo) % Mo);
}