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

题目描述是:

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. Python爬虫之requests库介绍(一)

    一:Requests: 让 HTTP 服务人类 虽然Python的标准库中 urllib2 模块已经包含了平常我们使用的大多数功能,但是它的 API 使用起来让人感觉不太好,而 Requests 自称 ...

  2. C#两个引用类的属性、方法 各位早安

    ***字符串.IndexOf("串"); - 返回字符串中第一个匹配项的索引,如果没有匹配项返回-1  intint b = s.IndexOf("天",s.I ...

  3. unittest—selenium自动化登录百度绕过校验

    这个脚本融合了unittest的校验,以及selenium的自动化,并且通过派发cookie信息成功绕过百度的验证码,并且利用装饰器成功只打开一次浏览器 #encoding=utf-8 from se ...

  4. 利用workbench对linux/Ubuntu系统中的mysql数据库进行操作

    在上一篇文章中,我分享了在linux中如何安装mysql数据库,但是这只是安装了mysql的服务,并没有图形化管理界面,所以这样子操作起来并没有那么方便,那么现在我们就来实现如何利用在window中安 ...

  5. Python 3 利用机器学习模型 进行手写体数字检测

    0.引言 介绍了如何生成手写体数字的数据,提取特征,借助 sklearn 机器学习模型建模,进行识别手写体数字 1-9 模型的建立和测试. 用到的几种模型: 1. LR,Logistic Regres ...

  6. Webrtc源码走读(一)

    阅读event_wrapper.h   event_wrapper_win.cpp 的实现 自己对“事件”这个词没有深的理解,通过看段代码,好像有点感觉,类似与C#的AutoResetEvent

  7. python-gevent模块(自动切换io的协程)

    2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 import gevent     def foo() ...

  8. oracle varchar2改成大字段类型clob,读取大字段内容

    http://blog.csdn.net/cai7095576/article/details/23999549

  9. Daily Scrumming* 2015.10.27(Day 8)

    一.总体情况总结 今日项目总结: 前后端同一了API设计以及API权限认证.用户状态保存的开发方案 API以及后端模型已经开始开发,前端UEditor开始学习,本周任务有良好的起步 前后端完成分工,后 ...

  10. 2018软工实践—Beta冲刺(5)

    队名 火箭少男100 组长博客 林燊大哥 作业博客 Beta 冲鸭鸭鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调组内工作 协助数据库完善搭建 展示GitHub当日代码/文档签入记 ...