题意:给出一个矩阵棋盘,大小不超过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. RabbitMQ service is already present - only updating service parameters

    如果你安装RabbitMQ不是那么一番顺利..那么你有可能会重装多次.. So..问题来了..重装时你执行   rabbitmq-service install  的时候..有可能就会报这个错了.. ...

  2. Java并发编程核心方法与框架-ScheduledExecutorService的使用

    类SchedukedExecutorService的主要作用是可以将定时任务与线程池功能结合. 使用Callable延迟运行(有返回值) public class MyCallableA implem ...

  3. gradle 默认属性

    Properties(未翻译) Property Description allprojects 包含该项目及其子项目的属性 ant The AntBuilder for this project. ...

  4. java中InvocationHandler 用于实现代理。

    以下的内容部分参考了网络上的内容,在此对原作者表示感谢! Java中动态代理的实现,关键就是这两个东西:Proxy.InvocationHandler,下面从InvocationHandler接口中的 ...

  5. AngularJs基础总结(1.4版本)

    注明:现在用的是最新的1系列1.4版本. 一.细节方面入手: 1,ng-app根节点,一般别写在html上面,最多也就写在body就行了,因为我们难免会忘记这个写在哪里,然后复制一些案例代码却总报错. ...

  6. [百度地图] ZMap 与 MultiZMap 封装类说明;

    ZMap.js 与 MultiZMap 说明 1. ZMap 与 MultiZMap 都是封装一些地图常用的使用方法,类方法功能大多使用 prototype 原型 实现: ZMap 在一个页面只能使用 ...

  7. PHP中的Memcache详解

    一.Memcache简介 Memcache是danga.com的一个项目,最早是为 LiveJournal 服务的,目前全世界不少人使用这个缓存项目来构建自己大负载的网站,来分担数据库的压力.它可以应 ...

  8. [译]Exploring Angular 1.3: Binding to Directive Controllers

    原文: http://blog.thoughtram.io/angularjs/2015/01/02/exploring-angular-1.3-bindToController.html Angul ...

  9. Oracle卸载

    用Oracle自带的卸载程序不能从根本上卸载Oracle,从而为下次的安装留下隐患,所以要想完全卸载Oracle就必须要直接将注册表清除. 步骤如下: 1. 开始->设置->控制面板-&g ...

  10. codevs5164 逆波兰表达式

    题目描述 Description 逆波兰表达式是一种把运算符前置的算术表达式(又叫前缀表达式),例如普通的表达式2 + 3的逆波兰表示法为+ 2 3.逆波兰表达式的优点是运算符之间不必有优先级关系,也 ...