Codeforces 348 D - Turtles

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-08-08 13:04:17

Codeforces 348 D - Turtles的相关文章

Codeforces Round #348

A. Little Artem and Presents 题意:Artem想将自己的n个糖果送给Masha,他想送尽量多的次数而不在乎数量,不顾每次送的数量不能和上次相同. 题解:只要第一次送一个,第二次送两个这样一次送就可以保证送的次数最多, 代码: 1 /*A*/ 2 #include<cstdio> 3 using namespace std; 4 5 int main() 6 { 7 int n; 8 while(scanf("%d",&n)!=EOF) 9

Codeforces Round #348 (VK Cup 2016 Round 2, Div. 1 Edition) C. Little Artem and Random Variable 数学

C. Little Artem and Random Variable Little Artyom decided to study probability theory. He found a book with a lot of nice exercises and now wants you to help him with one of them. Consider two dices. When thrown each dice shows some integer from 1 to

Codeforces Round #348(VK Cup 2016 - Round 2)

A - Little Artem and Presents (div2) 1 2 1 2这样加就可以了 #include <bits/stdc++.h> typedef long long ll; const int N = 1e5 + 5; int main() { int n; scanf ("%d", &n); int ans = n / 3 * 2; if (n % 3) { ans++; } printf ("%d\n", ans);

Codeforces 348B:Apple Tree(DFS+LCM+思维)

http://codeforces.com/contest/348/problem/B 题意:给一棵树,每个叶子结点有w[i]个苹果,每个子树的苹果数量为该子树所有叶子结点苹果数量之和,要使得每个结点的各个子树苹果数量相等,求至少需要拿走的苹果数量. 思路:一开始以为只要使得所有子树之和相同就行了. 1 void dfs(int u, int fa) { 2 int num = 0, mi = INF; 3 for(int i = head[u]; ~i; i = edge[i].nxt) {

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/

codeforces:Prefix Sums

题目大意: 给出一个函数P,P接受一个数组A作为参数,并返回一个新的数组B,且B.length = A.length + 1,B[i] = SUM(A[0], ..., A[i]).有一个无穷数组序列A[0], A[1], ... 满足A[i]=P(A[i-1]),其中i为任意自然数.对于输入k和A[0],求一个最小的下标t,使得A[t]中包含不小于k的数值. 其中A[0].length <= 2e5, k <= 1e18,且A[0]中至少有两个正整数. 数学向的题目.本来以为是个找规律的题目

codeforces:Helga Hufflepuff&#39;s Cup

题目大意:有一个包含n个顶点的无向无环连通图G,图中每个顶点都允许有一个值type,type的范围是1~m.有一个特殊值k,若一个顶点被赋值为k,则所有与之相邻的顶点只能被赋小于k的值.最多有x个顶点被赋值为k.求问有多少种不同的赋值方案. 这是一道树形DP的题目.由于是无环无向连通图,因此可以任选一个顶点R作为根结点,从而构造一颗树TREE.为每个顶点N维护一个属性maybe[3][x+1].其中maybe[0][i]表示当N被赋小于k的值时,N及其所有后代结点总共出现i个被赋值为k的结点的总

codeforces:855D Rowena Ravenclaw&#39;s Diadem分析和实现

题目大意: 提供n个对象,分别编号为1,...,n.每个对象都可能是某个编号小于自己的对象的特例或是成分.认为某个对象的特例的特例依旧是该对象的特例,即特例关系传递,同样一个对象的成分的成分依旧是该对象的成分.但是还需要注意一个对象的成分是该对象的所有特例的成分.每个对象都不可能是自己的特例或成分.要求解答之后的q个谜题,每个谜题提问两个对象是否是特例关系或成分关系. 输入级别:n,q<1e5 花了一个晚上才想到思路解了题目... 首先我们要先解决一个简单的问题,我将这道题目这样描述,对于一株多

【codeforces 718E】E. Matvey&#39;s Birthday

题目大意&链接: http://codeforces.com/problemset/problem/718/E 给一个长为n(n<=100 000)的只包含‘a’~‘h’8个字符的字符串s.两个位置i,j(i!=j)存在一条边,当且仅当|i-j|==1或s[i]==s[j].求这个无向图的直径,以及直径数量. 题解:  命题1:任意位置之间距离不会大于15. 证明:对于任意两个位置i,j之间,其所经过每种字符不会超过2个(因为相同字符会连边),所以i,j经过节点至多为16,也就意味着边数至多