@(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
4 2

Sample Output

1

Solution

題意:

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

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

轉載自sdchr博客

侵刪





代碼附上:

#include<cstdio>
#include<cctype>
using namespace std; inline int read()
{
int x = 0, flag = 1;
char c;
while(! isdigit(c = getchar()))
if(c == '-')
flag *= - 1;
while(isdigit(c))
x = x * 10 + c - '0', c = getchar();
return x * flag;
} void println(int x)
{
if(x < 0)
putchar('-'), x *= - 1;
if(x == 0)
putchar('0');
int ans[1 << 5], top = 0;
while(x)
ans[top ++] = x % 10, x /= 10;
for(; top; top --)
putchar(ans[top - 1] + '0');
putchar('\n');
} long long getQuantity(int x)
{
long long ret = 0; for(int i = 2; i <= x; i <<= 1)
ret += x / i; return ret;
} int calculate(int x, int y)
{
return getQuantity(x) - getQuantity(y) - getQuantity(x - y) == 0;
} int main()
{
int T = read(); while(T --)
{
int n = read(), m = read();
int d = n - m, oddQua = (m + 1) / 2;
println(calculate(d + oddQua - 1, oddQua - 1));
}
}

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. nordic芯片开发——烧写方法记录

    在开发nordic芯片的时候,分为存外设开发和结合softdevice开发,另外还有结合mbr的开发(这个暂时没有深究)在裸机开发的时候,sdk里面称为blank,把芯片的程序erase之后,直接下载 ...

  2. Hard problem CodeForces - 706C

    Time limit1000 ms Memory limit262144 kB 题目: Vasiliy is fond of solving different tasks. Today he fou ...

  3. 深入理解Python中的进程

    1.进程的概念什么是进程—>CPU在同一时刻只能处理一个任务,只是因为cpu执行速度很快. cpu在各个任务之间来回的进行切换. 进程的概念:正在进行的一个过程或者说一个任务,而负责执行任务的则 ...

  4. Ubuntu 档案权限

    Linux文件属性:查看指令是:ls -al ls是list的意思,重点在显示档案的文件名与相关属性.而选项-al则表示列出所有的档案详细的权限与属性.

  5. Go语言学习03

    Go语言-数组类型 一个数组(Array)就是一个可以容纳若干类型相同的元素的容器.这个容器的大小(即数组的长度)是固定的,且是体现在数组的类型字面量之中的.比如,我们声明了一个数组类型: type ...

  6. luogu2123 皇后游戏

    好题. 网上看到的范围是:\(T \leq 10\),$ n \leq 50000$, $ a_i,b_i \leq 10^9$. 我们按照贪心惯常的思路考虑交换相邻的两个人.容易发现,对于相邻的两个 ...

  7. fhqtreap - Luogu 2464 [SDOI2008]郁闷的小J

    [SDOI2008]郁闷的小JJ 题目描述 小J是国家图书馆的一位图书管理员,他的工作是管理一个巨大的书架.虽然他很能吃苦耐劳,但是由于这个书架十分巨大,所以他的工作效率总是很低,以致他面临着被解雇的 ...

  8. 初学-BeautifulSoup爬取豆瓣页面

    # -*- coding: utf-8 -*-import osimport urllibimport urllib2from bs4 import BeautifulSoup headers = { ...

  9. csrf 攻击及防御

    1.什么是CSRF攻击: CSRF(Cross-site request forgery),中文名称:跨站请求伪造,也被称为:one click attack/session riding,缩写为:C ...

  10. Jupyter Notebook与Jupyterhub的安装与配置

    Jupyter Notebook是一个很好用的交互环境,Jupyterhub则在此基础上实现了多用户的管理.最近配置这个环境的时候也遇到了一些坑,想想自己疯狂百度的过程,在此把自己的完整安装配置流程记 ...