D - Turtles
思路:
LGV 定理 (Lindstr?m–Gessel–Viennot lemma)
从{\(a_1\),\(a_2\),...,\(a_n\)} 到 {\(b_1\),\(b_2\),...,\(b_n\)}的不相交路径数等于行列式
\[
{\left[ \begin{array}{ccc}
c(a_1, b_1) & c(a_1, b_2) & ... & c(a_1, b_n) \c(a_2, b_1) & c(a_2, b_2) & ... & c(a_2, b_n) \... & ... & ... & ... \c(a_n, b_1) & c(a_n, b_2) & ... &c(a_n, b_n) \\end{array}
\right ]}
\]
的值。其中,\(c(a_i, b_i)\) 表示从点 \(a_i\) 到点 \(b_i\) 的路径方案数。
那么这道题就是求一个二阶行列式的值
代码:
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define y1 y11
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pli pair<LL, int>
#define pii pair<int, int>
#define piii pair<pii, int>
#define pdd pair<double, double>
#define mem(a, b) memset(a, b, sizeof(a))
#define debug(x) cerr << #x << " = " << x << "\n";
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//head
const int N = 3e3 + 5;
const int MOD = 1e9 + 7;
int dp[N][N], n, m;
char s[N][N];
int solve(int a, int b, int c, int d) {
for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) dp[i][j] = 0;
for (int i = a; i <= c; ++i) {
for (int j = b; j <= d; ++j) {
if(i == a && j == b) {
if(s[i][j] == '.') dp[i][j] = 1;
}
else {
if(s[i][j] == '.') dp[i][j] = (dp[i-1][j]+dp[i][j-1])%MOD;
}
}
}
return dp[c][d];
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; ++i) scanf("%s", s[i]+1);
printf("%lld\n", (solve(1, 2, n-1, m)*1LL*solve(2, 1, n, m-1) - solve(1, 2, n, m-1)*1LL*solve(2, 1, n-1, m)%MOD+MOD)%MOD);
return 0;
}
原文地址:https://www.cnblogs.com/widsom/p/11215383.html
时间: 2024-10-14 01:04:51