poj 1430 Binary Stirling Numbers

Binary Stirling
Numbers










Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 1761   Accepted: 671

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

Source

可以转化成求C(N,M)来做。当然不是直接转化。

打出表看一下,发现是有规律的。

每一列都会重复一次。打表看一下吧。

思路:

     s(n,m)   如果m是偶数  n=n-1;
m=m-1;==>转化到它的上一个s(n-1,m-1);

k=(m+1)/2;  n=n-k;
m=m-k;求C(n,m)的奇偶性就可以了。(当然有很多书写方式,不一定要这样做。)

测试用的

 1 #include<iostream>
2 #include<stdio.h>
3 #include<cstring>
4 #include<cstdlib>
5 using namespace std;
6
7 int dp[21][21];
8 int cnm[21][21];
9 void init()
10 {
11 int i,j;
12 dp[0][0]=1;
13 for(i=1;i<=20;i++) dp[i][0]=0;
14 for(i=1;i<=20;i++)
15 for(j=1;j<=i;j++)
16 dp[i][j]=dp[i-1][j-1]+dp[i-1][j]*j;
17 for(i=0;i<=15;i++)
18 {
19 for(j=0;j<=i;j++)
20 printf("%d ",(dp[i][j]&1));
21 printf("\n");
22 }
23
24 cnm[0][0]=1;
25 for(i=1;i<=20;i++)
26 {
27 cnm[i][0]=1;
28 cnm[0][i]=1;
29 }
30 for(i=1;i<=20;i++)
31 {
32 for(j=1;j<=i;j++)
33 {
34 if(j==1) cnm[i][j]=i;
35 else if(i==j) cnm[i][j]=1;
36 else cnm[i][j]=cnm[i-1][j]+cnm[i-1][j-1];
37 }
38 }
39 for(i=0;i<=15;i++)
40 {
41 for(j=0;j<=i;j++)
42 printf("%d ",cnm[i][j]&1);
43 printf("\n");
44 }
45 }
46 int main()
47 {
48 init();
49 int n,m;
50 while(scanf("%d%d",&n,&m)>0)
51 {
52 printf("%d\n",dp[n][m]);
53 }
54 return 0;
55 }

ac代码

 1 #include<iostream>
2 #include<stdio.h>
3 #include<cstring>
4 #include<cstdlib>
5 using namespace std;
6
7 int a[64],alen;
8 int b[64],blen;
9 void solve(int n,int m)
10 {
11 int i;
12 bool flag=false;
13 alen=0;
14 blen=0;
15 memset(a,0,sizeof(a));
16 memset(b,0,sizeof(b));
17 while(n)
18 {
19 a[++alen]=(n&1);
20 n=n>>1;
21 }
22 while(m)
23 {
24 b[++blen]=(m&1);
25 m=m>>1;
26 }
27 for(i=1; i<=alen; i++)
28 {
29 if(a[i]==0 && b[i]==1) flag=true;
30 if(flag==true) break;
31 }
32 if(flag==true)printf("0\n");
33 else printf("1\n");
34 }
35 int main()
36 {
37 int T;
38 int n,m,k;
39 scanf("%d",&T);
40 while(T--)
41 {
42 scanf("%d%d",&n,&m);
43 if(m%2==0)
44 {
45 n=n-1;
46 m=m-1;
47 }
48 k=(m+1)/2;
49 solve(n-k,m-k);
50 }
51 return 0;
52 }

poj 1430 Binary Stirling Numbers,布布扣,bubuko.com

时间: 2024-10-15 08:22:34

poj 1430 Binary Stirling Numbers的相关文章

[POJ 1430] Binary Stirling Number

题意 $\left\{ \begin{aligned} n \\ k \end{aligned} \right\} \mod 2$ . $n, k \le 10 ^ 9$ . 分析 $\left\{ \begin{aligned} 0 \\ 0 \end{aligned} \right\} = 1$ . $\left\{ \begin{aligned} n \\ k \end{aligned} \right\} = k \left\{ \begin{aligned} n-1 \\ k \end{

Binary Stirling Numbers

http://poj.org/problem?id=1430 题目: 求 第二类 斯特林数 的 奇偶性  即 求 s2 ( n , m ) % 2 : 题解: https://blog.csdn.net/ez_2016gdgzoi471/article/details/80219736 #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<iostre

POJ 1385-Binary Stirling Numbers(判断第二类斯特林数的奇偶性)

Binary Stirling Numbers Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1430 Appoint description:  System Crawler  (2015-04-22) Description The Stirling number of the second kind S(n, m) stands

POJ 2499 Binary Tree 题解

本题使用所谓的辗转相除法. 还需要逆过来遍历二叉树.可以想象给出的数据点是根节点,然后遍历到根节点(1,1). 考的是根据给出的规则,总结规律的能力. #include <stdio.h> namespace BinaryTree2499_1 { int main() { int T, a, b, le, ri; scanf("%d", &T); for (int t = 1; t <= T; t++) { scanf("%d %d", &

POJ 1785 Binary Search Heap Construction (线段树)

题目大意: 给出的东西要求建立一个堆,使得后面的数字满足堆的性质,而且字符串满足搜索序 思路分析: 用线段树的最大询问建树.在建树之前先排序,然后用中序遍历递归输出. 注意输入的时候的技巧... #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #define lson num<<1,s,mid #define rson num<<

poj 2769 Reduced ID Numbers(memset使用技巧)

Description T. Chur teaches various groups of students at university U. Every U-student has a unique Student Identification Number (SIN). A SIN s is an integer in the range 0 ≤ s ≤ MaxSIN with MaxSIN = 106-1. T. Chur finds this range of SINs too larg

poj 1430 第二类斯特林数

1 #include <iostream> 2 #include <cmath> 3 #include <algorithm> 4 using namespace std; 5 6 int get2(long long n){ 7 if(n==0) 8 return 0; 9 int cnt =0; 10 while(n){ 11 cnt += n/2; 12 n = n/2; 13 } 14 return cnt; 15 } 16 int main(){ 17 18

POJ 2769 Reduced ID Numbers

思路: 枚举 Reduced ID Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8847 Accepted: 3552 Description T. Chur teaches various groups of students at university U. Every U-student has a unique Student Identification Number (SIN). A SIN s

POJ 1995 Raising Modulo Numbers (数论-整数快速幂)

Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4379   Accepted: 2516 Description People are different. Some secretly read magazines full of interesting girls' pictures, others create an A-bomb in their cellar, oth