• 如果喜欢本站，您可以按CTRL+D收藏本站，方便下次访问。

• 欢迎来自五湖四海的朋友，期待为您分享有价值的资源

# POJ 1430 – Binary Stirling Numbers 斯特灵数

10年前 (2010-08-28) 190次浏览

Binary Stirling Numbers

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 1040 Accepted: 346

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

Central Europe 2001

#include <stdio.h>
int main(int argc, char *argv[])
{
int n,m;
int z,w1,w2;
int t;
int a,b,c;
scanf("%d",&t);

while (t--)
{
scanf("%d%d",&n,&m);
z=n-(m+2)/2;
w1=(m-1)/2;
w2=z-w1;
a=0;
while (z)
{
z>>=1;
a+=z;
}
b=0;
while (w1)
{
w1>>=1;
b+=w1;
}
c=0;
while (w2)
{
w2>>=1;
c+=w2;
}
printf("%d\n",(a-b-c)==0);

}

return 0;
}


====
• 版权声明

本站的文章和资源来自互联网或者站长的原创，按照 CC BY-NC-SA 4.0 CN协议发布和共享，转载或引用本站文章应保留来源网址，并遵循相同协议。如果有侵犯版权的资源请尽快联系站长，我们会在24H内删除有争议的资源。
• 网站驱动

• 友情链接

• 支持一下