zoukankan      html  css  js  c++  java
  • 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) 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;
    }
    
  • 相关阅读:
    jquery 序列化form表单
    nginx for windows 安装
    nodejs idea 创建项目 (一)
    spring 配置 shiro rememberMe
    idea 2018 解决 双击shift 弹出 search everywhere 搜索框的方法
    redis 在windows 集群
    spring IOC控制反转和DI依赖注入
    redis 的安装
    shiro 通过jdbc连接数据库
    handlebars的用法
  • 原文地址:https://www.cnblogs.com/suncongbo/p/11282070.html
Copyright © 2011-2022 走看看