LOJ #2109. 「JLOI2015」骗我呢 数形结合+组合+容斥
神仙题!!!!
其实还是有一点没有搞懂,但是大体上差不多懂了.
code:
#include <bits/stdc++.h> #define ll long long #define N 3200020 #define setIO(s) freopen(s".in","r",stdin) using namespace std; const int mod=1e9+7; int n,m; ll fac[N],inv[N]; void Init() { fac[0]=fac[1]=inv[0]=inv[1]=1; int i,j; for(i=2;i<N;++i) { fac[i]=(ll)i*fac[i-1]%mod; inv[i]=(mod-mod/i)*inv[mod%i]%mod; } for(i=1;i<N;++i) inv[i]=inv[i]*inv[i-1]%mod; } ll C(int x,int y) { if(x<y||x<0||y<0) return 0; return fac[x]*inv[y]%mod*inv[x-y]%mod; } ll calc(int x,int y) { if(x<0||y<0) return 0; return C(x+y,y); } void turn_A(int &x,int &y) { swap(x,y); x--,y++; } void turn_B(int &x,int &y) { swap(x,y); x+=m+2,y-=m+2; } ll ans; int main() { // setIO("input"); int i,j; scanf("%d%d",&n,&m); Init(); ans=calc(n+m+1,n); int x=n+m+1,y=n; while(x>=0&&y>=0) { turn_A(x,y),ans=(ans-calc(x,y)+mod)%mod; turn_B(x,y),ans=(ans+calc(x,y)+mod)%mod; } x=n+m+1,y=n; while(x>=0&&y>=0) { turn_B(x,y),ans=(ans-calc(x,y)+mod)%mod; turn_A(x,y),ans=(ans+calc(x,y)+mod)%mod; } printf("%lld\n",ans); return 0; }