@(POJ)[Stirling數, 排列組合, 數形結合]

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

Sample Output

  1. 1

Solution

題意:

求斯特林數$$ \left{ \begin{array}{} n \ k \end{array}{} \right} % 2$$$$n, m \in [1, 10^9]$$

這題直接求解肯定是會T的, 因此考慮優化.

轉載自sdchr博客

侵刪





代碼附上:

  1. #include<cstdio>
  2. #include<cctype>
  3. using namespace std;
  4. inline int read()
  5. {
  6. int x = 0, flag = 1;
  7. char c;
  8. while(! isdigit(c = getchar()))
  9. if(c == '-')
  10. flag *= - 1;
  11. while(isdigit(c))
  12. x = x * 10 + c - '0', c = getchar();
  13. return x * flag;
  14. }
  15. void println(int x)
  16. {
  17. if(x < 0)
  18. putchar('-'), x *= - 1;
  19. if(x == 0)
  20. putchar('0');
  21. int ans[1 << 5], top = 0;
  22. while(x)
  23. ans[top ++] = x % 10, x /= 10;
  24. for(; top; top --)
  25. putchar(ans[top - 1] + '0');
  26. putchar('\n');
  27. }
  28. long long getQuantity(int x)
  29. {
  30. long long ret = 0;
  31. for(int i = 2; i <= x; i <<= 1)
  32. ret += x / i;
  33. return ret;
  34. }
  35. int calculate(int x, int y)
  36. {
  37. return getQuantity(x) - getQuantity(y) - getQuantity(x - y) == 0;
  38. }
  39. int main()
  40. {
  41. int T = read();
  42. while(T --)
  43. {
  44. int n = read(), m = read();
  45. int d = n - m, oddQua = (m + 1) / 2;
  46. println(calculate(d + oddQua - 1, oddQua - 1));
  47. }
  48. }

POJ1430 Binary Stirling Numbers的更多相关文章

  1. poj 1430 Binary Stirling Numbers

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

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

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

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

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

  4. 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) ...

  5. Binary Stirling Numbers

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

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

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

  7. acm数学(转)

    这个东西先放在这吧.做过的以后会用#号标示出来 1.burnside定理,polya计数法    这个大家可以看brudildi的<组合数学>,那本书的这一章写的很详细也很容易理解.最好能 ...

  8. [转] POJ数学问题

    转自:http://blog.sina.com.cn/s/blog_6635898a0100magq.html 1.burnside定理,polya计数法 这个大家可以看brudildi的<组合 ...

  9. ACM数学

     1.burnside定理,polya计数法 这个专题我单独写了个小结,大家可以简单参考一下:polya 计数法,burnside定理小结 2.置换,置换的运算 置换的概念还是比较好理解的,< ...

随机推荐

  1. writing a usb driver(在国外的网站上复制下来的)

    Writing a Simple USB Driver   From Issue #120April 2004 Apr 01, 2004  By Greg Kroah-Hartman  in Soft ...

  2. debian安装之后使用android手机上网

    安装debian的过程中,没有连接网线.因为路由器在客厅,电脑在卧室,拖条长长的线很不方便. 断网安装完成之后,通过usb连上i9250. 在i9250上,执行以下操作: “设置”--->“更多 ...

  3. JS实现——贪吃蛇

    把以下代码保存成Snake.html文件,使用Google或360浏览器打开 <!DOCTYPE HTML> <html> <head> <meta char ...

  4. Leetcode 430.扁平化多级双向链表

    扁平化多级双向链表 您将获得一个双向链表,除了下一个和前一个指针之外,它还有一个子指针,可能指向单独的双向链表.这些子列表可能有一个或多个自己的子项,依此类推,生成多级数据结构,如下面的示例所示. 扁 ...

  5. [转]查看Linux版本信息

    一.查看Linux内核版本命令(两种方法): 1.cat /proc/version [root@S-CentOS home]# cat /proc/version Linux version 2.6 ...

  6. Spring c3p0连接池通过Hibernate配置

    首先进行Hibernate配置,详见http://www.cnblogs.com/claricre/p/6509931.html 然后调用这三个包. 配置hibernate.cfg.xml文件: &l ...

  7. [POJ1155]TELE

    [POJ1155]TELE 试题描述 A TV-network plans to broadcast an important football match. Their network of tra ...

  8. 浅谈android反调试之轮询TracePid(解决方案是特色)

    参考文章: 1.  http://bbs.pediy.com/thread-207538.htm 2.  http://www.wjdiankong.cn/android 需求: 常见的Android ...

  9. Windows上安装DB2——从IBM官网得到90天试用版

    我在下面选的90天试用版: https://www.ibm.com/developerworks/cn/downloads/im/db2/ 进入下载页面,选择Windows https://www-0 ...

  10. [转] Makefile 基础 (2) —— Makefile 总述

    该篇文章为转载,是对原作者系列文章的总汇加上标注. 支持原创,请移步陈浩大神博客:(最原始版本) http://blog.csdn.net/haoel/article/details/2886 我转自 ...