这个题目初看上去是一个排列组合题,而实际上……也是一个排列组合题。

题目描述是:

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
 根据题目给出的递推公式,

S(n, m) = m S(n - 1, m) + S(n - 1, m - 1), for n, m > 0
显然
  对于m为偶数,S(n,m)=S(n-1,m-1);
  m为奇数,S(n,m)=S(n-1,m)+S(n-1,m-1)=S(n-1,m)+S(n-2,m-2)(m为奇数,则m-2为偶数)
这样的话就没有m这个系数,得到一个递推。
但是问题又来了,这个题目n和m都太大,如果是直接地递推,肯定是承受不住的。
那么就要想想别的办法,在网上看到有人讲到的方法就是根据画图,什么意思呢?
以n为x轴,m为y轴
数形结合,当m为偶数的时候可以变为m-1,当m为奇数的时候可以变为m和m-2,这样就是全部是奇数了。
同时又一个关键的地方需要理解。求S(n,m)就是求又多上条路径从(0,0)到(n,m)仔细理解一下。
这样的话思路又变化到排列组合上来。上面说到所有的偶数m都会变成m-1(奇数),同时对于奇数(n,m)可以变为(n-1,m)和(n-2,m-2)(其实就是两条路)
这样的话就是总共有多少种走法呢。
总共要横着走n-m步,斜着走(即从n,m变为n-2,m-2)(m-1)/2步。
总用要走n-m+(m-1)/2步,
这样总路径数就是C(n-m+(m-1)/2,(m-1)/2)。
于是听过欧拉函数的类似性质就可以迅速地得到所求的答案了呢。
我的代码:
#include <iostream>
#include <cstdio>
using namespace std; int d,n,m; int count(int x)
{
return x==0?0:x/2+count(x/2);
} int main()
{
scanf("%d",&d);
while (d--)
{
scanf("%d%d",&n,&m);
n-=m,m=(m-1)/2;
if (count(n+m)==count(n)+count(m)) puts("1");
else puts("0");
}
return 0;
}

POJ1430的更多相关文章

  1. POJ1430 Binary Stirling Numbers

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

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

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

  3. HDU_1430 魔板 【BFS+康托展开+置换】

    一.题面 POJ1430 二.分析 该题与之前做的八数码不同,它是一个2*4的棋盘,并且没有空的区域.这样考虑的情况是很少的,依然结合康托展开,这时康托展开最多也只乘7的阶乘,完全可以BFS先预处理一 ...

随机推荐

  1. 百度ueditor 文本框

    所需配置(qui框架) <!--ueEditor编辑器start--> <script>  window.UEDITOR_HOME_URL = ctx+"/stati ...

  2. 将exe依赖运行的dll,合并入exe中,整个程序仅存在一个exe文件

    方法一: 使用ILMerge合并winform生成的exe和引用的dll文件 参考:https://blog.csdn.net/u010108836/article/details/76782375 ...

  3. .Net 如何访问主流的各大数据库

    做过开发的都知道,.NET基本可以理解是和MSSQL,windows服务器属于一个好的搭档,正如PHP和MYSQL,LIUNX等也可以理解是一个完美搭配:但是在实际的开发中并不完全是这样的,如果你是学 ...

  4. C#多线程的几种使用

    参见链接   :http://www.jb51.net/article/46234.htm

  5. PLSQL触发器,游标

    --触发器 drop table emp_log create table emp_log( empno number, log_date date, new_salary number, actio ...

  6. Linux重定向与管道

    程序执行时默认会打开3个流,标准输入.标准输出.标准错误. Redirection The shell interprets the symbols <,>, and >> a ...

  7. Streamr助你掌控自己的数据(3)——教你在Streamr市场上发布数据

    博客说明 所有刊发内容均可转载但是需要注明出处. 教你在Streamr市场上发布数据 本系列文档主要介绍怎么通过Streamr管理自己的DATA,整个系列包括三篇教程文档,分别是:教你5分钟上传数据至 ...

  8. mongodb基本使用(三)

    MongoDB 创建数据库 语法 MongoDB 创建数据库的语法格式如下: use DATABASE_NAME 如果数据库不存在,则创建数据库,否则切换到指定数据库. 如果你想查看所有数据库,可以使 ...

  9. Vue 事件处理

    原生的js事件处理 原生的js事件处理,可以分为:直接内联执行代码,或者绑定事件函数. 在内联的事件处理函数内部或者事件绑定的方法内部的作用域中的this都是指向当前的dom对象.如何在vue绑定的元 ...

  10. 作业 20181127-3 互评Beta版本

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2448 组名:可以低头,但没必要 组长:付佳 组员:张俊余 李文涛 孙赛佳 ...