This page is still under construction.

Parts of this page are still being built. What you see may change.

Pattern Generator

Interview

Time limit1sMemory limit128 MB

Summary
For each (n, k) pair, print all n-bit strings with exactly k ones in decreasing numeric order, separated by blank lines.
Level

Medium5 of 10

Topics
Backtracking, Recursion, Bit manipulation, Combinatorics
Solved
No attempts yet

Problem

Write a program that, for each given pair n and k, prints every bit pattern of length n that contains exactly k ones. The patterns are printed in descending order of their value when interpreted as binary numbers. The input contains several n, k pairs, and the task is repeated for each pair.

Input

The first line contains T, the number of n, k pairs. Each pair consists of two integers n and k separated by a single space. Every input satisfies 0<n≤300 < n \le 30, 0≤k<80 \le k < 8, and n≥kn \ge k.

Output

For each n, k pair, first print the line The bit patterns are, then print every valid bit pattern in descending order, one per line. Each pattern must be printed with exactly n digits, including leading zeroes. Separate the outputs of two consecutive pairs with a single blank line.

Examples1

  1. Example 1

    Input
    3
    2 1
    2 0
    4 2
    
    Expected output
    The bit patterns are
    10
    01
    
    The bit patterns are
    00
    
    The bit patterns are
    1100
    1010
    1001
    0110
    0101
    0011