Luogu5363 SDOI2019移动金币(博弈+动态规划)
容易想到可以转化为一个有m堆石子,石子总数不超过n-m的阶梯博弈。阶梯博弈的结论是相当于只考虑奇数层石子的nim游戏。
nim和不为0不好算,于是用总方案数减掉nim和为0的方案数。然后考虑dp,按位考虑,设f[i][j]为已确定奇数石子堆的第i位及以上的放法后,保证当前异或和为0,剩下j个石子时的方案数。转移套一些组合数即可。
#include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; #define ll long long #define P 1000000009 #define N 150100 char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;} int gcd(int n,int m){return m==0?n:gcd(m,n%m);} int read() { int x=0,f=1;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f; } int n,m,fac[N],inv[N],f[20][N],ans=1; int C(int n,int m){if (m>n) return 0;return 1ll*fac[n]*inv[m]%P*inv[n-m]%P;} int F(int n,int m){return C(n+m-1,m-1);} int main() { #ifndef ONLINE_JUDGE freopen("a.in","r",stdin); freopen("a.out","w",stdout); const char LL[]="%I64d\n"; #else const char LL[]="%lld\n"; #endif n=read(),m=read(); fac[0]=1;for (int i=1;i<=n+m;i++) fac[i]=1ll*fac[i-1]*i%P; inv[0]=inv[1]=1;for (int i=2;i<=n+m;i++) inv[i]=P-1ll*(P/i)*inv[P%i]%P; for (int i=2;i<=n+m;i++) inv[i]=1ll*inv[i-1]*inv[i]%P; ans=C(n,m);n-=m; f[19][n]=1; for (int i=18;~i;i--) for (int j=0;j<=n;j++) for (int k=0;j+(2*k<<i)<=n&&2*k<=(m+1>>1);k++) f[i][j]=(f[i][j]+1ll*f[i+1][j+(2*k<<i)]*C(m+1>>1,2*k)%P)%P; for (int i=0;i<=n;i++) ans=(ans+P-1ll*f[0][i]*F(i,(m>>1)+1)%P)%P; cout<<ans; return 0; }