BINSTIRL  Binary Stirling Numbers
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 fourelement 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 you 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(n1, m) + S(n1, m1), 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.
For instance, S(4, 2) mod 2 = 1.
Task
Write a program that:
 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 ith data set  exactly two integers n_{i} and m_{i} separated by a single space, 1 < = m_{i} < = n_{i} <= 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(n_{i}, m_{i}) mod 2.
Example
Sample input: 1 4 2 Sample output: 1
hide comments
cake_is_a_lie:
20170303 14:06:11
OK, challenge accepted and completed: AC with O(1), without help from teh internets. For me, visual representation was key. Last edit: 20170303 14:07:12 

rahulpadhy:
20160824 04:56:14
How to derive the formula of the parity of the stirling numbers of the second kind that works in O(1) time ?


gautam:
20160426 15:07:54
O(1)...;) 

Vishesh Middha:
20150815 14:49:57
providing SIGSEGV error why please explain??


(Tjandra Satria Gunawan)(æ›¾æ¯…æ˜†):
20150728 05:54:23
Solve this problem without taking help from net? Challenge accepted ;) 

ANIKET:
20150727 07:13:59
Last edit: 20150727 07:15:44 

Rajat (1307086):
20141228 23:04:26
Challenge for those who do not know Binary Stirling numbers:


sunil gowda:
20141220 09:51:15
how to do in O(1) time .. anyone knows..


parijat bhatt:
20141002 17:12:20
@ j1k7_7(JaskamalKainth)


Anubhav Balodhi :
20140813 17:02:56
Sierpinski Gasket does it all ^_^ 
Added by:  adrian 
Date:  20040702 
Time limit:  3s 
Source limit:  50000B 
Memory limit:  1536MB 
Cluster:  Cube (Intel G860) 
Languages:  All 
Resource:  ACM Central European Programming Contest, Warsaw 2001 