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

题目描述是:

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. HTML中内联元素与块状元素介绍

    常用的块级元素: address , center , div , dl ,, form , h1 , h2 , h3 , h4 , h5 , h6 , menu , ol , p , table , ...

  2. UWP DEP0700: 应用程序注册失败。[0x80073CF9] Install failed. Please contact your software vendor. (Exception from HRESULT: 0x80073CF9)

    现在部署的app项目八成是从以前的一个项目复制过来,修改的.或者本地存在一个相同的app没有卸载. 解决方法: 1. 卸载之前相同的app 2.如果是复制的项目A,但是经过修改后变成了项目B,并且A和 ...

  3. C++构造函数深度探究

    1.引子: 以下代码中的输出语句输出0吗,为什么? struct Test { int _a; Test(int a) : _a(a) {} Test() { Test(0); } }; Test o ...

  4. Nginx应用场景

    1. Nginx应用场景 1)http服务器.Nginx可以独立的提供http服务,可以做网页静态服务器(也就是将静态文件放到nginx目录下,通过nginx来访问就ok)   2)虚拟主机,可以在一 ...

  5. 多个EXCEL文件合并成一个

    Python的numpy处理起来会比较方便,有空实现一下,这里是Excel内部代码的方式: 合并方法如下: 1.需要把多个excel表都放在同一个文件夹里面,并在这个文件夹里面新建一个excel.如图 ...

  6. Java SE练习题——求奇数

    欢迎来到Java SE练习题频道,我是Fishing,今天我带来的练习题是(做题会有不足之处,可评论,说出更好的方法): 通过键盘输入两个整数,计算这两个整数之间的所有奇数之和,并输出计算结果. 看到 ...

  7. vue 组件-父组件传值给子组件

    父组件通过属性,传值给子组件,子组件通过,props数组里的名称来接受父组件传过来的值. HTML部分: <div id="app"> <tmp1 :parent ...

  8. Netty源码分析第6章(解码器)---->第2节: 固定长度解码器

    Netty源码分析第六章: 解码器 第二节: 固定长度解码器 上一小节我们了解到, 解码器需要继承ByteToMessageDecoder, 并重写decode方法, 将解析出来的对象放入集合中集合, ...

  9. Codeforces Round #524 (Div. 2) C. Masha and two friends(矩形相交)

    C. Masha and two friends time limit per test 1 second memory limit per test 256 megabytes input stan ...

  10. 用线性分类器实现预测鸢尾花的种类(python)

    这是个人学习时跑的代码,结果就不贴了,有需要的可以自己运行,仅供参考,有不知道的可以私下交流,有问题也可以联系我.当然了我也只能提供一点建议,毕竟我也只是初学者 第一个页面 # -*- coding: ...