hdu_5794_A Simple Chess(lucas+dp)
2016-08-06 01:27
295 查看
题意:
给你n,m,从(1,1)到(n,m),每次只能从左上到右下走日字路线,有k(<=100)的不能走的位置,问你有多少方案
题解:
画图可看到路线是一个杨辉三角的图,然后我们可以将对应的x,y转换到对应的点上,也可以吧杨辉三角看成一个平行四边形,
我这里看成的平行四边形,设dp[i]为从起点到第i个障碍物的的方案数,那么dp[i]=dp[i]-sum(dp[j](第j个点能走到i这个点)*(j到i的方案数))。
然后我们把终点放到最后,最后求出的dp[end]就是答案
#include<cstdio> #include<algorithm> #define F(i,a,b) for(int i=a;i<=b;i++) using namespace std; typedef long long ll; const ll P=110119; ll f[P+5],r[P+5]; ll C(ll n,ll m){return n<m?0:f *r[n-m]%P*r[m]%P;} ll lucas(ll n,ll m){ if(n<m||m<0)return 0; if(!m||n==m)return 1; return C(n%P,m%P)*lucas(n/P,m/P)%P; } void init(){ int i; for(r[0]=r[1]=f[0]=f[1]=1,i=2;i<P;i++){ f[i]=f[i-1]*i%P,r[i]=-r[P%i]*(P/i)%P; while(r[i]<0)r[i]+=P; } for(i=2;i<P;i++)r[i]=r[i]*r[i-1]%P; } ll n,m,k,ic=1,dp[110]; struct point{ ll x,y; bool operator<(const point &b)const{return x<b.x;} }p[110]; int main(){ init(); while(~scanf("%lld%lld%lld",&n,&m,&k)) { n--,m--; int fg=0; F(i,1,k) { scanf("%lld%lld",&p[i].x,&p[i].y),p[i].x--,p[i].y--; if(p[i].x==n&&p[i].y==m)fg=1; } printf("Case #%lld: ",ic++); if(n==0&&m==0)puts("1"); else if((n+m)%3!=0||fg)puts("0"); else { ll x,y,tx,ty; sort(p+1,p+1+k); p[++k].x=n,p[k].y=m; F(i,1,k) { if((p[i].x+p[i].y)%3==0) { x=(p[i].x+p[i].y)/3,y=min(p[i].x,p[i].y)-x; dp[i]=lucas(x,y); F(j,1,i-1) { if(p[j].y<p[i].y&&p[j].x<p[i].x) { ll xx=p[i].x-p[j].x,yy=p[i].y-p[j].y; if((xx+yy)%3==0) { ll tx=(xx+yy)/3,ty=min(xx,yy)-tx; dp[i]-=(lucas(tx,ty)*dp[j])%P; dp[i]=(dp[i]+P)%P; } } } } } printf("%lld\n",dp[k]); } } return 0; }View Code
相关文章推荐
- HDU-5794 <2016 Multi-University Training 6> A Simple Chess (Lucas + DP)
- HDU 5794 A Simple Chess (dp+Lucas组合数取模)
- HDU 5794 A Simple Chess (Lucas + dp)
- HDU 5794 A Simple Chess (容斥原理+Lucas定理+dp)
- HDU 5794 A Simple Chess dp+Lucas
- HDU 5794 A Simple Chess 16多校6 dp 容斥 lucas
- HDU_5794_ASimpleChess(Lucas定理&&(容斥||dp))
- HDU 5794 A Simple Chess (容斥+Lucas定理)
- HDU 5794 A Simple Chess (容斥+DP+Lucas)
- HDU 5794 A Simple Chess (lucas定理+费马小定理)
- hdu 5794 A Simple Chess 【lucas+容斥】
- HDU 5794 A Simple Chess (容斥+lucas定理)
- HDU 5794-B - A Simple Chess - DP
- (HDU 5794)2016 Multi-University Training Contest 6 A Simple Chess (Lucas、容斥)
- HDU 5794 A Simple Chess Lucas大数组合数取模
- hdu 5794 A Simple Chess(Lucas 定理)
- HDU 5794 A Simple Chess(多校,dp,容斥)
- 找规律+Lucas定律_______A Simple Chess(hdu 5794 2016多校第六场)
- HDU 5794 A Simple Chess(卢卡斯定理 + 容斥原理)
- HDU 5794 A Simple Chess