题意:给出一个矩阵棋盘,大小不超过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. [转] MemCached 的 stats 命令

    Memcached有个stats命令,通过它可以查看Memcached服务的许多状态信息.使用方法如下:先在命令行直接输入telnet 主机名端口号,连接到memcached服务器,然后再连接成功后, ...

  2. array_map与array_column之间的关系

    /*|----------------------------------------------------------|array_map();将回调函数作用到给定数组的单元上|array_col ...

  3. PHP 学习

    http://www.w3school.com.cn/php/php_sessions.asp public static void chkacc() {Response.Redirect(" ...

  4. 『MySQL』索引类型 normal, unique, full text

    问题1:mysql索引类型normal,unique,full text的区别是什么? normal:表示普通索引 unique:表示唯一的,不允许重复的索引,如果该字段信息保证不会重复例如身份证号用 ...

  5. linux下代替system的基于管道的popen和pclose函数

    linux下使用system需要谨慎,那么代替它的方法是什么呢? 标准I/O函数库提供了popen函数,它启动另外一个进程去执行一个shell命令行. 这里我们称调用popen的进程为父进程,由pop ...

  6. CF460C Present (二分 + 差分数列)

    Codeforces Round #262 (Div. 2) C C - Present C. Present time limit per test 2 seconds memory limit p ...

  7. Django URLconf

    Django提供了干净优雅的 URL 方案,URL配置文件是一个标准的 python 文件,支持动态配置.它的本质就是URL模式与调用的视图函数之间的映射表,最简单的配置文件如下: from djan ...

  8. ubuntu 14.04 安装mysql server的分支MariaDB Server初级教程

    序,MariaDB Server是Mysql的fork版本,与Mysql完美兼容,mysql在10年被sun收购,后sun被oracle收购,后mysql的创建者及项目长期技术带头人之一的Michae ...

  9. Jquery控制滚动显示欢迎字幕v2

    Jquery控制滚动显示欢迎字幕v2: 之前做的那个比较适合测试环境,但要套入到网站中,有两个按钮在那摆着,还是不太好看.后面对代码进行了修改,如下: 参考代码: <html> <h ...

  10. jquery 使用textarea

    问题: 若在textarea标签中键入一个回车时,将插入2个字符\n\r ,将在datagrid不能正确显示. \n: 回车符 \r: 换行符  解决方案: txt = txt.Replace(&qu ...