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. {1, 2, 3} U {4}, {1, 2, 4} U {3}, {1, 3, 4} U {2}, {2, 3, 4} U {1}

  2. {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.

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

  2. 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

  1. 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. 1
  2. 4 2

Sample Output

  1. 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)的奇偶性就可以了。(当然有很多书写方式,不一定要这样做。)

测试用的

  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<cstring>
  4. #include<cstdlib>
  5. using namespace std;
  6.  
  7. int dp[][];
  8. int cnm[][];
  9. void init()
  10. {
  11. int i,j;
  12. dp[][]=;
  13. for(i=;i<=;i++) dp[i][]=;
  14. for(i=;i<=;i++)
  15. for(j=;j<=i;j++)
  16. dp[i][j]=dp[i-][j-]+dp[i-][j]*j;
  17. for(i=;i<=;i++)
  18. {
  19. for(j=;j<=i;j++)
  20. printf("%d ",(dp[i][j]&));
  21. printf("\n");
  22. }
  23.  
  24. cnm[][]=;
  25. for(i=;i<=;i++)
  26. {
  27. cnm[i][]=;
  28. cnm[][i]=;
  29. }
  30. for(i=;i<=;i++)
  31. {
  32. for(j=;j<=i;j++)
  33. {
  34. if(j==) cnm[i][j]=i;
  35. else if(i==j) cnm[i][j]=;
  36. else cnm[i][j]=cnm[i-][j]+cnm[i-][j-];
  37. }
  38. }
  39. for(i=;i<=;i++)
  40. {
  41. for(j=;j<=i;j++)
  42. printf("%d ",cnm[i][j]&);
  43. printf("\n");
  44. }
  45. }
  46. int main()
  47. {
  48. init();
  49. int n,m;
  50. while(scanf("%d%d",&n,&m)>)
  51. {
  52. printf("%d\n",dp[n][m]);
  53. }
  54. return ;
  55. }

ac代码

  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<cstring>
  4. #include<cstdlib>
  5. using namespace std;
  6.  
  7. int a[],alen;
  8. int b[],blen;
  9. void solve(int n,int m)
  10. {
  11. int i;
  12. bool flag=false;
  13. alen=;
  14. blen=;
  15. memset(a,,sizeof(a));
  16. memset(b,,sizeof(b));
  17. while(n)
  18. {
  19. a[++alen]=(n&);
  20. n=n>>;
  21. }
  22. while(m)
  23. {
  24. b[++blen]=(m&);
  25. m=m>>;
  26. }
  27. for(i=; i<=alen; i++)
  28. {
  29. if(a[i]== && b[i]==) flag=true;
  30. if(flag==true) break;
  31. }
  32. if(flag==true)printf("0\n");
  33. else printf("1\n");
  34. }
  35. int main()
  36. {
  37. int T;
  38. int n,m,k;
  39. scanf("%d",&T);
  40. while(T--)
  41. {
  42. scanf("%d%d",&n,&m);
  43. if(m%==)
  44. {
  45. n=n-;
  46. m=m-;
  47. }
  48. k=(m+)/;
  49. solve(n-k,m-k);
  50. }
  51. return ;
  52. }

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. 最近兰州的js风格写个插件和一个template engine

    /* *@Product Name: Rational Framework Author: Calos Description: pager !important: pager */ (functio ...

  2. Java的正则表达式

    package RegexTest; /** * Created by hu on 2016/3/29. */ /* * Java的正则表达式 在正则表达式中,用\d表示一位数字,如果在其它语言中使用 ...

  3. linux第2天 信号 wait

    孤儿进程和僵尸进程 如果父进程先退出,子进程还没退出那么子进程的父进程将变为init进程.(注:任何一个进程都必须有父进程) 如果子进程先退出,父进程还没退出,那么子进程必须等到父进程捕获到了子进程的 ...

  4. 五种常见的电子商务模式对比:B2B、B2C、C2B、C2C、O2O

    电子商务模式是指企业运用互联网开展经营取得营业收入的基本方式,也就是指在网络环境中基于一定技术基础的商务运作方式和盈利模式.目前,常见的电子商务模式主要有B2B.B2C.C2B.C2C.O2O等几种, ...

  5. PAT乙级 1022. D进制的A+B (20)

    1022. D进制的A+B (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 输入两个非负10进制整数A和 ...

  6. android环境搭建—— 工欲善其事必先利其器

    安卓开发环境配置: 准备软件 a)  JDK  点击下载 b)   adt-bundle-windows-x86-20140702.zip   点击下载 [sdk + 特定版本platform + e ...

  7. iBatis叙述

    1.添加Mybatis的配置文件conf.xml 在src目录下创建一个conf.xml文件,如下图所示: 2.定义表所对应的实体类 3.定义操作users表的sql映射文件userMapper.xm ...

  8. java中的拷贝(一)

    摘自:http://blog.csdn.net/tounaobun/article/details/8491392 假如说你想复制一个简单变量.很简单: int apples = 5; int pea ...

  9. 【转】Web UI自动化测试原理

    目前市面上有很多Web UI自动化测试框架,比如WatiN, Selinimu,WebDriver,还有VS2010中的Coded UI等等.  这些框架都可以操作Web中的控件,模拟用户输入,点击等 ...

  10. JS货币数字转换中文

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...