POJ 1430 Binary Stirling Numbers (第二类斯特林数、组合计数)

题目链接

http://poj.org/problem?id=1430

题解

qaq写了道水题……

在模\(2\)意义下重写一下第二类Stirling数的递推式: \[S(n,m)=S(n-1,m-1)+(S(n-1,m)\ \text{and}\ m)\]
\(S'(n,m)=S(n+m,m)\), 那么递推式变成了\(S'(n,m)=S'(n,m-1)+(S'(n-1,m)\ \text{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\n"); continue;}
        n -= m; m = (m-1)>>1;
        printf("%d\n",comb(n+m,m));
    }
    return 0;
}

转载于:https://www.cnblogs.com/suncongbo/p/11282070.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值