Poj 1430 Binary Stirling Numbers

Source: Internet
Author: User

The second type of Stirling is the number of methods that divide a set containing n elements into k non-empty subsets.
Recurrence formula:
S (n, k) = 0 (n <k | k = 0 ),
S (n, n) = S (n, 1) = 1,
S (n, k) = S (n-1, k-1) + kS (n-1, k ).

The following figure is taken from Wikipedia. There is a conversion formula for the binary Stringer number and the combination number. However, it is easy to combine the remainder of the number 2.

 

We know that the combination number C (N, M) = N! /M! /(N-M )!, Therefore, the problem can be solved simply by finding the two duplicates in the factorial prime factor factorization formula.
N! The number of duplicates after the prime factor decomposition can be calculated in the following formula.
K = N/2 + N/2 ^ 2 + N/2 ^ 3 + ....

 

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 shoshould consist of exactly d lines, one line for each data set. line I, 1 <= I <= d, shoshould contain 0 or 1, the value of S (ni, mi) mod 2.
Sample Input

1
4 2
Sample Output

1
 

Code:

[Cpp]
# Include <iostream>
Using namespace std;
Int f (int k)
{
Int a = 0;
While (k)
{
K = k/2;
A + = k;
}
Return;
}
Int main ()
{
Int t, n, m, z, w1, w2;
Scanf ("% d", & t );
While (t --)
{
Scanf ("% d", & n, & m );
Z = n-(m + 2)/2;
W1 = m-1)/2;
W2 = z-w1;
If (f (z) = f (w1) + f (w2 ))
Printf ("1% \ n ");
Else printf ("0% \ n ");
}
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.