您的位置:首页 > 其它

HDU 5794-B - A Simple Chess - DP

2016-08-08 21:20 309 查看
与此题类似
http://blog.csdn.net/viphong/article/details/50603204
dp[i]表示(1,1)到第i个石头的方案数(之间无障碍)

然后如果 (1,1)与i有障碍,那么dp[i]-=dp[j]* (i到j的方案数),这就减去了所有包含j的路径数,得到(1,1)到点i,不经过之间所有障碍的方案数。

最后把n,m也看作一个障碍,dp[r+1]得到就是答案

计算(1,1)到第i个石头的方案数(之间无障碍)

可以通过打表找到规律

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iostream>
using namespace std;

const double pi=acos(-1.0);
double eps=0.000001;
typedef long long ll;

long long fac[1000010];
const ll mod=110119;
long long fast_m(long long a,long long b)
{
long long x,ans=1;
x=a;
while(b)
{
if (b&1)
ans=(ans*x)%mod;
x=(x*x)%mod;
b/=2;
}
return ans;
}

long long Lucas(long long n,long long m )
{
long long ret=1;
while(n&&m)
{
long long a=n%mod,b=m%mod;
if(a<b) return 0;
ret=(ret*fac[a]*fast_m(fac[b]*fac[a-b]%mod,mod-2))%mod;
n/=mod;
m/=mod;
}
return ret;
}

struct node
{
ll x ,y;
};
node aa[105];

bool cmp(node a,node b)
{
if (a.x!=b.x)
return a.x<b.x;
else
return a.y<b.y;
}
ll get(ll x,ll y)
{
if ((x+y)%3!=2) return 0;
ll c=(x+y)/3;
ll ans;
if ((x+y)%2==0)
{
ll center=(x+y)/2;
ll cc=c/2;
ll dis=abs(center-x);
if (cc-dis<0) return 0;
ans=Lucas(c,cc-dis);
}
else
{
ll center1=(x+y)/2;
ll center2=center1+1;
ll c1=c/2;
ll c2=c/2+1;
ll dis1=abs(center1-x);
ll dis2=abs(center2-x);
if (dis1<dis2)
{
if (c1-dis1<0) return 0;
ans=Lucas(c,c1-dis1);
}
else
{
if (c2+dis2>c) return 0;
ans=Lucas(c,c2+dis2);
}
}
return ans;
}
ll dp[105];
int main()
{
fac[0]=1;
for( int i=1; i<=110119; i++)
fac[i]=fac[i-1]*i%mod;
/*printf("%lld\n",fac[110119]);
printf("%lld\n",fac[110110]);
printf("%lld\n",fac[110112]);
printf("%lld\n",fac[110005]);*/
/* for (int i=1;i<=20;i++)
{
for (int j=1;j<=20;j++)
{
ll ans=get(i,j);
printf("%lld ",ans);
}
printf("\n");
}*/

long long n,m;
int r;
int cnt=1;
while (scanf("%lld%lld%d",&n,&m,&r)!=EOF)
{
memset(dp,0,sizeof dp);
for (int i=1; i<=r; i++)
scanf("%lld%lld",&aa[i].x,&aa[i].y);
printf("Case #%d: ",cnt++);
sort(aa+1,aa+1+r,cmp);
if (aa[r].x==n&&aa[r].y==m)
{
printf("0\n");
continue;
}

aa[r+1].x=n,aa[r+1].y=m;
r++;
for (int i=1; i<=r; i++)
{
dp[i]=get(aa[i].x,aa[i].y);
for (int j=1; j<i; j++)
{
if (aa[j].x<=aa[i].x&&aa[j].y<=aa[i].y)
{
dp[i]= dp[i]-get(aa[i].x-aa[j].x+1,aa[i].y-aa[j].y+1)%mod*(dp[j]) %mod;
dp[i]=(dp[i]+mod)%mod;
}
}
}
printf("%lld\n",dp[r]);
}
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: