题目链接
http://poj.org/problem?id=1430
题解
qaq写了道水题……
在模(2)意义下重写一下第二类Stirling数的递推式: $$S(n,m)=S(n-1,m-1)+(S(n-1,m) ext{and} m)$$
令(S'(n,m)=S(n+m,m)), 那么递推式变成了(S'(n,m)=S'(n,m-1)+(S'(n-1,m) ext{and} m))
也就相当于从((0,0))走到((n,m))的NE Lattice Path数目,且当纵坐标为偶数时只能往上走不能往右走
那这个只能往上走不能往右走就相当于把这一行删掉了(因为对方案没有任何影响),于是保留下来的行只有([frac{m-1}{2}])个
那么就是从((0,0))走到((n,[frac{m-1}{2}]))的NE Lattice Path条数,直接Lucas定理组合数计算即可
(m=0)要特判
时间复杂度(O(T(log n+log m))).
代码
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cassert>
#include<iostream>
#define llong long long
using namespace std;
inline int read()
{
int x=0; bool f=1; char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=0;
for(; isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+(c^'0');
if(f) return x;
return -x;
}
int comb0(int x,int y) {return x<y?0:1;}
int comb(int x,int y)
{
if(x<2&&y<2) {return comb0(x,y);}
return comb((x>>1),(y>>1))*comb0((x&1),(y&1));
}
int main()
{
int T; scanf("%d",&T);
while(T--)
{
int n,m; scanf("%d%d",&n,&m);
if(m==0) {printf("0
"); continue;}
n -= m; m = (m-1)>>1;
printf("%d
",comb(n+m,m));
}
return 0;
}