Binary Stirling Numbers
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 1761   Accepted: 671

Description

The Stirling number of the second kind S(n, m) stands for the number of ways to partition a set of n things into m nonempty subsets. For example, there are seven ways to split a four-element set into two parts:

{1, 2, 3} U {4}, {1, 2, 4} U {3}, {1, 3, 4} U {2}, {2, 3, 4} U {1}

{1, 2} U {3, 4}, {1, 3} U {2, 4}, {1, 4} U {2, 3}.

There is a recurrence which allows to compute S(n, m) for all m and n.

S(0, 0) = 1; S(n, 0) = 0 for n > 0; S(0, m) = 0 for m > 0;

S(n, m) = m S(n - 1, m) + S(n - 1, m - 1), for n, m > 0.

Your task is much "easier". Given integers n and m satisfying 1 <= m <= n, compute the parity of S(n, m), i.e. S(n, m) mod 2.

Example

S(4, 2) mod 2 = 1.

Task

Write a program which for each data set: 
reads two positive integers n and m, 
computes S(n, m) mod 2, 
writes the result. 

Input

The first line of the input contains exactly one positive integer d equal to the number of data sets, 1 <= d <= 200. The data sets follow.

Line i + 1 contains the i-th data set - exactly two integers ni and mi separated by a single space, 1 <= mi <= ni <= 10^9.

Output

The output should consist of exactly d lines, one line for each data set. Line i, 1 <= i <= d, should contain 0 or 1, the value of S(ni, mi) mod 2.

Sample Input

1
4 2

Sample Output

1

Source

可以转化成求C(N,M)来做。当然不是直接转化。

打出表看一下,发现是有规律的。

每一列都会重复一次。打表看一下吧。

思路:

     s(n,m)   如果m是偶数  n=n-1; m=m-1;==>转化到它的上一个s(n-1,m-1);

k=(m+1)/2;  n=n-k; m=m-k;求C(n,m)的奇偶性就可以了。(当然有很多书写方式,不一定要这样做。)

测试用的

 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
using namespace std; int dp[][];
int cnm[][];
void init()
{
int i,j;
dp[][]=;
for(i=;i<=;i++) dp[i][]=;
for(i=;i<=;i++)
for(j=;j<=i;j++)
dp[i][j]=dp[i-][j-]+dp[i-][j]*j;
for(i=;i<=;i++)
{
for(j=;j<=i;j++)
printf("%d ",(dp[i][j]&));
printf("\n");
} cnm[][]=;
for(i=;i<=;i++)
{
cnm[i][]=;
cnm[][i]=;
}
for(i=;i<=;i++)
{
for(j=;j<=i;j++)
{
if(j==) cnm[i][j]=i;
else if(i==j) cnm[i][j]=;
else cnm[i][j]=cnm[i-][j]+cnm[i-][j-];
}
}
for(i=;i<=;i++)
{
for(j=;j<=i;j++)
printf("%d ",cnm[i][j]&);
printf("\n");
}
}
int main()
{
init();
int n,m;
while(scanf("%d%d",&n,&m)>)
{
printf("%d\n",dp[n][m]);
}
return ;
}

ac代码

 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
using namespace std; int a[],alen;
int b[],blen;
void solve(int n,int m)
{
int i;
bool flag=false;
alen=;
blen=;
memset(a,,sizeof(a));
memset(b,,sizeof(b));
while(n)
{
a[++alen]=(n&);
n=n>>;
}
while(m)
{
b[++blen]=(m&);
m=m>>;
}
for(i=; i<=alen; i++)
{
if(a[i]== && b[i]==) flag=true;
if(flag==true) break;
}
if(flag==true)printf("0\n");
else printf("1\n");
}
int main()
{
int T;
int n,m,k;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
if(m%==)
{
n=n-;
m=m-;
}
k=(m+)/;
solve(n-k,m-k);
}
return ;
}

poj 1430 Binary Stirling Numbers的更多相关文章

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

    题目链接 http://poj.org/problem?id=1430 题解 qaq写了道水题-- 在模\(2\)意义下重写一下第二类Stirling数的递推式: \[S(n,m)=S(n-1,m-1 ...

  2. poj 1430 Binary Stirling Number 求斯特林数奇偶性 数形结合| 斯特林数奇偶性与组合数的关系+lucas定理 好题

    题目大意 求子集斯特林数\(\left\{\begin{matrix}n\\m\end{matrix}\right\}\%2\) 方法1 数形结合 推荐一篇超棒的博客by Sdchr 就是根据斯特林的 ...

  3. POJ1430 Binary Stirling Numbers

    @(POJ)[Stirling數, 排列組合, 數形結合] Description The Stirling number of the second kind S(n, m) stands for ...

  4. Binary Stirling Numbers

    http://poj.org/problem?id=1430 题目: 求 第二类 斯特林数 的 奇偶性  即 求 s2 ( n , m ) % 2 : 题解: https://blog.csdn.ne ...

  5. UVALIVE 2431 Binary Stirling Numbers

    转自别人的博客.这里记录一下 这题是定义如下的一个数: S(0, 0) = 1; S(n, 0) = 0 for n > 0;S(0, m) = 0 for m > 0; S(n, m) ...

  6. 【poj1430】Binary Stirling Numbers(斯特林数+组合数)

    传送门 题意: 求\(S(n,m)\% 2\)的值,\(n,m\leq 10^9\),其中\(S(n,m)\)是指第二类斯特林数. 思路: 因为只需要关注奇偶性,所以递推式可以写为: 若\(m\)为偶 ...

  7. POJ 3252:Round Numbers

    POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...

  8. 笛卡尔树 POJ ——1785 Binary Search Heap Construction

    相应POJ 题目:点击打开链接 Binary Search Heap Construction Time Limit: 2000MS   Memory Limit: 30000K Total Subm ...

  9. POJ - 3252 A - Round Numbers

    The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, Paper, Stone' ...

随机推荐

  1. SQL top查询

    select *from emp;

  2. JSP out乱码

    在form method="post" 写post 在接受页面jsp代码前面加request.setCharacterEncoding("UTF-8");

  3. linux kernel.shmall shemax shemin 參數解釋

    分类: oracle linux 2010-06-17 14:30 6193人阅读 评论(0) 收藏 举报 linuxoracleredhat数据库服务器x86 Linux X86-64操作系统,Or ...

  4. paper 68 :MATLAB中取整函数(fix, floor, ceil, round)的使用

    MATLAB取整函数 1)fix(x) : 截尾取整. >> fix( [3.12 -3.12]) ans =      3    -3 (2)floor(x):不超过x 的最大整数.(高 ...

  5. 【sublime】在终端下手动安装sublime text 2

    Sublime2下载地址:http://www.sublimetext.com/download step.1 解压下载的压缩包 tar xf Sublime\ Text\ 2.0.2.tar.bz2 ...

  6. C#和JavaScript交互(asp.net前台和后台互调)总结 (转)

    http://www.cnblogs.com/poleices/archive/2011/02/24/1963727.html C#代码与javaScript函数的相互调用: 1.如何在JavaScr ...

  7. (function($){...})(jQuery) 函数详解

    function(arg){...} 这是一个匿名函数,参数是arg. 而调用匿名函数时,是在函数后面写上括号和实参的,由于操作符的优先级,函数本身也需要用括号,即: function(arg){.. ...

  8. Xutils请求服务器json数据与下载文件

    String code_url = "https://ic.snssdk.com/user/mobile/send_code/v2/"; HttpUtils httpUtils = ...

  9. 视频处理控件TVideoGrabber部分技术问题解答

    TVideoGrabber是一个功能全面.易于使用的视频捕捉工具和多媒体播放器,本文搜集了一些TVideoGrabber的技术问答,并针对于有的朋友遇到的疑难给出了解答. 一.在TVideoGrabb ...

  10. COM编程概述

    所谓COM,英文为Componet Object Model,中文为组件对象模型(其实这种解释只有在考试卷上才具有一点实际意义). [1]为什么需要COM? COM是为了解决OLE问题而产生的.COM ...