题意:给出一个矩阵棋盘,大小不超过10^5。上面有n个非法点(n<=2000)不可以踩,从左上角开始走到右下角,每次只能向下或者向右移动。问有多少种走法。结果对10^9+7取模。

分析:

组合数学DP

设dp[i]表示从左上角开始不经过非法点走到第i个非法点有多少种方法。

dp[i]=num(s, point[i]) - sigma( dp[j] * num(point[j], point[i]) );

其中,sigma表示加和,s是起始点,j遍历所有在点i左上方的点。num(a,b)表示从a到b有多少种走法(不用避开非法点)。

num可以直接用组合数表示,即c(x+y-2, x -1)。

这道题涉及到了较大的组合数取模问题。求大组合数方法如下:

可以先处理出所有1~n的阶乘,存入数组。factorial[i]表示i的阶乘取模后的结果。

并求出所有的阶乘在该模运算下的逆元,存入数组。inverse[i]表示factorial[i]的逆元。

c(n,m)原本等于( n! / (n-m)! ) / m!,现在用这两个数组可以表示为factorial[n] * inverse[n-m] * inverse[m]。

模板如下:

#define LL long long

const int MAX_M = (int)(2e5) + ;
const int MOD = (int)(1e9) + ; LL multi_mod(LL a, LL b, LL c)
{ //返回(a*b) mod c,a,b,c<2^63
a %= c;
b %= c;
LL ret = ;
while (b)
{
if (b & )
ret = (ret + a) % c;
a <<= ;
a %= c;
b >>= ;
}
return ret;
} LL pow_mod(LL x, LL n, LL mod)
{ //返回x^n mod c ,非递归版
x %= mod;
if (n == )
return x;
LL ret = ;
while (n)
{
if (n & )
ret = multi_mod(ret, x, mod);
n >>= ;
x = multi_mod(x, x, mod);
}
return ret;
} long long get_inverse(long long a)
{
return pow_mod(a, MOD - , MOD);
} long long factorial[MAX_M];
long long inverse[MAX_M]; void init_comb(int n)
{
factorial[] = ;
for (int i = ; i <= n; i++)
{
factorial[i] = factorial[i - ] * i;
factorial[i] %= MOD;
}
inverse[n] = get_inverse(factorial[n]);
for (int i = n - ; i >= ; i--)
{
inverse[i] = inverse[i + ] * (i + );
inverse[i] %= MOD;
}
} long long comb(long long a, long long b)
{
long long ret = factorial[a] * inverse[a - b];
ret %= MOD;
ret *= inverse[b];
ret %= MOD;
return ret;
}

求逆元可以使用扩展欧几里德算法,但是比较麻烦。这次我使用了一个更为简单的算法,费马小定理。

即:当p是质数且a和p互质,那么a^(p-1)=1 (mod p)

而逆元的定义是x * y=1 (mod p)则y是x的逆元。令x=a,且a与p互质,则由a^(p-1)=1 (mod p)可得:y=a^(p-2)。

对于求a的逆元这个问题,a<p且p是质数,自然可以利用上面的结论,a的逆元就是a^(p-2)。

求逆元模板如下:

LL multi_mod(LL a, LL b, LL c)
{ //返回(a*b) mod c,a,b,c<2^63
a %= c;
b %= c;
LL ret = ;
while (b)
{
if (b & )
ret = (ret + a) % c;
a <<= ;
a %= c;
b >>= ;
}
return ret;
} LL pow_mod(LL x, LL n, LL mod)
{ //返回x^n mod c ,非递归版
x %= mod;
if (n == )
return x;
LL ret = ;
while (n)
{
if (n & )
ret = multi_mod(ret, x, mod);
n >>= ;
x = multi_mod(x, x, mod);
}
return ret;
} long long get_inverse(long long a)
{
return pow_mod(a, MOD - , MOD);
}

本题代码如下:

#include <cstdio>
#include <algorithm>
using namespace std; #define d(x)
#define LL long long const int MAX_N = ;
const int MAX_M = (int)(2e5) + ;
const int MOD = (int)(1e9) + ; int row_num, col_num;
int n;
pair<int, int> point[MAX_N];
long long dp[MAX_N]; void input()
{
scanf("%d%d%d", &row_num, &col_num, &n);
for (int i = ; i < n; i++)
{
int a, b;
scanf("%d%d", &a, &b);
point[i] = make_pair(a, b);
}
point[n++] = make_pair(row_num, col_num);
point[n++] = make_pair(, );
for (int i = ; i < n; i++)
{
point[i].first--;
point[i].second--;
}
} LL multi_mod(LL a, LL b, LL c)
{ //返回(a*b) mod c,a,b,c<2^63
a %= c;
b %= c;
LL ret = ;
while (b)
{
if (b & )
ret = (ret + a) % c;
a <<= ;
a %= c;
b >>= ;
}
return ret;
} LL pow_mod(LL x, LL n, LL mod)
{ //返回x^n mod c ,非递归版
x %= mod;
if (n == )
return x;
LL ret = ;
while (n)
{
if (n & )
ret = multi_mod(ret, x, mod);
n >>= ;
x = multi_mod(x, x, mod);
}
return ret;
} long long get_inverse(long long a)
{
return pow_mod(a, MOD - , MOD);
} long long factorial[MAX_M];
long long inverse[MAX_M]; void init_comb(int n)
{
factorial[] = ;
for (int i = ; i <= n; i++)
{
factorial[i] = factorial[i - ] * i;
factorial[i] %= MOD;
}
inverse[n] = get_inverse(factorial[n]);
for (int i = n - ; i >= ; i--)
{
inverse[i] = inverse[i + ] * (i + );
inverse[i] %= MOD;
}
} long long comb(long long a, long long b)
{
long long ret = factorial[a] * inverse[a - b];
ret %= MOD;
ret *= inverse[b];
ret %= MOD;
return ret;
} int main()
{
init_comb();
input();
sort(point, point + n);
dp[] = ;
for (int i = ; i < n; i++)
{
dp[i] = comb(point[i].first + point[i].second, point[i].second);
for (int j = ; j < i; j++)
{
if (point[j].first <= point[i].first && point[j].second <= point[i].second)
{
long long a = point[i].first - point[j].first;
a += point[i].second - point[j].second; long long b = point[i].second - point[j].second; dp[i] -= (dp[j] * comb(a, b)) % MOD;
dp[i] += MOD;
dp[i] %= MOD;
}
}
d(printf("dp[%d] = %d\n", i, (int)dp[i]));
}
printf("%d\n", (int)dp[n - ]);
return ;
}

ccf559c的更多相关文章

随机推荐

  1. PHP 函数整理 (用过的)

    1:$_SERVER['DOCUMENT_ROOT'] $_SERVER['DOCUMENT_ROOT']是PHP预定义的几个变量之一.作用是:获取当前运行脚本所在的文档根目录.该根目录是由服务器配置 ...

  2. jQuery parent.append和$after的区别

    首先假设我们有个id为test的div和一个id为test2的div: <div id="test">     我是测试div </div> <div ...

  3. html兼容性

    IE  property:value\9; //for all IE IE6 _property:value; IE7 *property:value; IE8 +property:value; IE ...

  4. ESI 动态缓存技术

    任何一个Web网站的内容都是在不断更新和变化,但这并不意味这这个网站的内容就是动态内容,事实上,动态的内容是指用户每次点击 相同的链接时取的的内容是由Web服务器应用程序生成的,如常见得ASP,JSP ...

  5. bestcoder Round #7 前三题题解

    BestCoder Round #7 Start Time : 2014-08-31 19:00:00    End Time : 2014-08-31 21:00:00Contest Type : ...

  6. hdu4975 A simple Gaussian elimination problem.(正确解法 最大流+删边判环)(Updated 2014-10-16)

    这题标程是错的,网上很多题解也是错的. http://acm.hdu.edu.cn/showproblem.php?pid=4975 2014 Multi-University Training Co ...

  7. 2015年---移动端webapp知识总结

    没想到这样又过了5个月了,近期辞职了,所以我有时间来做总结. 这段时间里我学习了很多东西,而且都是我们移动端webapp的同学值得去学习的. 我先告诉大家我这次写的总结,有以下内容: 1.body的背 ...

  8. [译]git clone

    git clone git clone命令copy一个已经存在的Git仓储. git clone有点像svn的checkout, 他的不同之处是这个copy也是一个完整的仓储-它有自己的历史纪录, 能 ...

  9. iOS开发关于Block代码错误

    本文永久地址为http://www.cnblogs.com/ChenYilong/p/4052362.html ,转载请注明出处. iOS开发关于Block代码错误 Incompatible bloc ...

  10. 绿书模拟day10 单词前缀

    [题目描述]一组单词是安全的,当且仅当不存在一个单词是另一个单词的前缀,这样才能保证数据不容易被误解,现在你手上有一个单词集合s,你需要计算有多少个自己是安全的.注意空集永远是安全的.[输入格式]第一 ...