POJ 1385-Binary Stirling Numbers(判断第二类斯特林数的奇偶性)
来源:互联网 发布:java 对象初始化 编辑:程序博客网 时间:2024/05/14 07:35
Binary Stirling Numbers
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64uAppoint description:
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:
There is a recurrence which allows to compute S(n, m) for all m and n.
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
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.
{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.
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
14 2
题意:
将n个连续的数分成m个非空的部分的分法:满足公式S(0,0)=1, S(n,0)=0, S(0,m)=0,S(n,m) = mS(n-1,m) + S(n-1, m-1), 求S(n,m)%2的值
分析: 设f(n, m)=S(n,m)%2, 则当m为偶数时,f(n,m)=f(n-1,m-1), 当m为奇数时,f(n,m)=f(n-1,m)+f(n-1,m-1)。建立以n为x轴,以m为y轴,根据以上的公式算出从(0,0)到(n,m)的路径,得出f(n,m)=C((n-m), (m-1)/2),要求f(n,m)%2,根据C(n,m)=n!/(m!(n-m)!),若C(n,m)的分母中2因子的个数等于分子中2因子的个数,则有C(n,m)为奇数,否则C(n,m)为偶数
详细解析:点击打开链接
#include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <sstream>#include <algorithm>#include <set>#include <queue>#include <stack>#include <map>using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const double pi= acos(-1.0);#define lson l,mid,rt<<1#define rson mid+1,r,rt<<1|1int cnt_2(int x)//x!中2因子的个数{ int cnt=0; while(x){ x/=2; cnt+=x; } return cnt;}int main(){ int T,n,m; scanf("%d",&T); while(T--){ scanf("%d %d",&n,&m); n=n-m; m=(m-1)/2; if(cnt_2(n+m)==cnt_2(n)+cnt_2(m))//如果分子分母2的个数相等则为奇数 puts("1"); else//否则为偶数 puts("0"); } return 0;}
1 0
- POJ 1385-Binary Stirling Numbers(判断第二类斯特林数的奇偶性)
- poj 1430 Binary Stirling Numbers
- poj 1430 Binary Stirling Numbers 题解
- POJ 1430 Binary Stirling Numbers (斯特林数)
- poj 1430 Binary Stirling Numbers 斯特灵数结论
- (Relax 数论1.31)POJ 1430 Binary Stirling Numbers(斯特灵数与组合数的转换)
- POJ题目1430 Binary Stirling Numbers(数学,斯特林)
- 【模板】第二类斯特林数Stirling
- 判断n的奇偶性
- C - Binary Stirling Numbers解题报告(来自网络)
- 组合数奇偶性的判断
- 组合数奇偶性的判断
- 判断一个数的奇偶性
- 组合数的奇偶性判断
- 判断一个数的奇偶性
- 判断整数奇偶性的函数
- 判断一个整数的奇偶性
- 5.2判断数的奇偶性
- Problem 39—Integer right triangles
- 如何制作ear包
- LS,MMSE,LMMSE,ML,MAP,LMS,AR,MSE误差介绍
- linux常用命令收集
- MFC中使用ActiveX的WebBrowser载入动态网页
- POJ 1385-Binary Stirling Numbers(判断第二类斯特林数的奇偶性)
- Android学习 - 单击事件另两种实现方法
- css浏览器适配
- 欢迎使用CSDN-markdown编辑器
- 位运算总结
- Mina初级使用
- 类似于INI格式的配置文件的加载及解析
- Java IO学习笔记
- FZU2184【逆序数还原】