HDU 5794 A Simple Chess (Lucas + dp)

时间:2023-03-09 12:51:27
HDU 5794 A Simple Chess (Lucas + dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5794

多校这题转化一下模型跟cf560E基本一样,可以先做cf上的这个题。

题目让你求一个棋子开始在(1,1),只能像马一样走且往右下方走,不经过坏点,有多少种走法能到达(n,m)点。

比如n=6, m=5 有两个坏点,模型转换 如下图:

HDU 5794 A Simple Chess (Lucas + dp)

转换成简单模型之后,只要把棋子可能经过的坏点存到结构体中,按照x与y从小到大排序。

dp[i]表示从起点到第i个坏点且不经过其他坏点的方案数。

dp[i] = Lucas(x[i], y[i]) - sum(dp[j]*Lucas(x[i]-x[j], y[i]-x[j])) , x[j] <= x[i] && y[j] <= y[i] //到i点所有的路径数目 - 经过其他点的路径数目

那我们把最后一个点也设成坏点,比如dp[final],那么dp[final]就是答案了

 //#pragma comment(linker, "/STACK:102400000, 102400000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
typedef pair <LL, LL> P;
const int N = 1e2 + ;
const LL mod = ;
struct data {
LL x, y;
bool operator <(const data& cmp) const {
return x == cmp.x ? y < cmp.y : x < cmp.x;
}
}q[N];
LL f[mod + ]; //阶乘
dp[N]; LL Pow(LL a , LL n , LL mod) {
LL res = ;
while(n) {
if(n & )
res = res * a % mod;
a = a * a % mod;
n >>= ;
}
return res;
} LL Comb(LL a , LL b , LL mod) {
if(a < b) {
return ;
}
if(a == b) {
return ;
}
return (f[a] * Pow(f[a - b]*f[b] % mod , mod - , mod)) % mod; //乘法逆元
} LL Lucas(LL n , LL m , LL mod) {
LL ans = ;
while(m && n && ans) {
ans = (ans * Comb(n % mod , m % mod , mod)) % mod;
n /= mod;
m /= mod;
}
return ans;
} bool judge(LL x, LL y) { //判断'马' 能否走到坏点
if(x > y)
swap(x, y);
LL dec = y - x;
if(dec > x || dec* > y || (x - dec) % != || (y - dec*) % != || x - dec != y - dec*)
return true;
return false;
} P get(LL x, LL y) { //得到模型的x和y
P res;
if(x > y) {
LL dec = x - y;
res.first = dec, res.second = ;
x -= dec * , y -= dec;
res.first += x / , res.second += y / ;
} else {
LL dec = y - x;
res.first = , res.second = dec;
x -= dec, y -= dec * ;
res.first += x / , res.second += y / ;
}
return res;
} int main()
{
int Case = ;
LL n, m;
int k;
f[] = ;
for(LL i = ; i <= mod; ++i)
f[i] = f[i - ] * i % mod;
while(scanf("%lld %lld %d", &n, &m, &k) != EOF) {
n--, m--;
for(int i = ; i <= k; ++i) {
scanf("%lld %lld", &q[i].x, &q[i].y);
q[i].x--, q[i].y--;
}
printf("Case #%d: ", Case++);
if(judge(n, m)) {
printf("0\n");
continue;
}
P temp = get(n, m);
n = temp.first, m = temp.second;
int index = ;
for(int i = ; i <= k; ++i) {
if(judge(q[i].x, q[i].y))
continue;
temp = get(q[i].x, q[i].y);
if(temp.first > n || temp.second > m)
continue;
q[++index].x = temp.first, q[index].y = temp.second;
}
sort(q + , q + index + );
q[++index].x = n, q[index].y = m;
memset(dp, , sizeof(dp));
for(int i = ; i <= index; ++i) {
LL sum = ;
for(int j = ; j < i; ++j) {
if(q[i].x >= q[j].x && q[i].y >= q[j].y) {
sum = (sum + dp[j]*Lucas(q[i].x - q[j].x - q[j].y + q[i].y, q[i].y - q[j].y, mod) % mod) % mod;
}
}
dp[i] = ((Lucas(q[i].x + q[i].y, q[i].y, mod) - sum) % mod + mod) % mod;
}
printf("%lld\n", dp[index]);
}
return ;
}