
# chi -- characteristic (indicator) vector
# is a plain binary vector
# nextChi returns T/F if next vector exists

def nextChi( chi ):
    j = len(chi)-1
    while True:
        if j < 0: return False
        if chi[j] == 1:
            chi[j] = 0
            j -= 1
        else:
            chi[j] = 1
            return True


# next k-subset of n elements {1,2,3,....n}
# in lexicographical ordering of subsets
# presuming k = length(subset array)
def nextSubset( subset,  n ):
    j = len(subset)-1
    maxval = n
    while True:
        if j < 0: return False
        if subset[j] == maxval:
            # move left
            maxval -= 1
            j -= 1
        else:
            subset[j] += 1
            for j2 in range(j+1, len(subset)):
                subset[j2] = subset[j2-1]+1
            return True

# prev k-subset of n elements {1,2,3,....n}
# in lexicographical ordering of subsets
# presuming k = length(subset array)
def prevSubset( subset, n ):
    j = len(subset)-1
    # try to move right
    while True:
        if j == 0:
            if subset[j] == 1: return False
            else: break
        else:
            if subset[j-1] + 1 < subset[j]: break
            else: j -= 1
    subset[j] -= 1
    # fill the values to the right of j
    val = n
    for j2 in range(len(subset)-1, j, -1):
        subset[j2] = val
        val -= 1
    return True


def binCoeff( n, k ):
    res = 1
    for i in range(0,k): # from 0 to k-1
        res = res * (n-i)
        res = res // (i+1)
    return res

def countSubsets( minVal, maxVal, k ):
    return binCoeff( maxVal-minVal+1, k )



# recurse along k
def rankSubsetpr( subset, n ):
    #print( subset)
    k = len(subset)
    if k == 1: return subset[0] - 1

    rank = 0
    # total number of all subsets containing
    # values 1, 2, ..., subset[0]-1, which precede the given subset
    # in the list of all subsets lexicographically sorted
    #print( n )
    for i in range(1, subset[0]):
        #print( "binco", n-i, k-1)
        rank += binCoeff( n-i, k-1 )

    # exclude first elem from the subset array
    # and "normalize" the input for recursion
    subset1 = subset[1:] # copy of subset[1..k]
    #print(subset1)
    for j in range( len(subset1) ):
        subset1[j] -= subset[0]
    n1 = n - (subset[0])
    #print(subset1)
    #print( rank )
    #print()
    # and recurse
    return rank + rankSubsetpr( subset1, n1)


def unrankSubset ( rank, n, k ):
    if k == 1:
        return( [rank+1])

    # jump over appropriate number of blocks
    # which precede the subset with the given rank
    # and simultaneously construct value subset[0]
    blockSizes = 0
    n1 = n-1
    subset0 = 1
    while True:
        bSize = binCoeff( n1, k-1 )
        if bSize <= rank:
            rank -= bSize
            subset0 += 1
            n1 -= 1
        else: break

    subsetRec = unrankSubset ( rank, n1, k-1 )
    print(k, n, rank,  subset0, subsetRec )
    print()
    for j in range( len(subsetRec) ):
        subsetRec[j] += subset0

    #return concatenation
    return [subset0] + subsetRec



def nextPerm( perm ):
    n = len( perm )
    j = n-1 # last index
    # 1. find shortest non-descending suffix of length  >= 2
    while True:
        if j == 0: return False # no next permutation
        if perm[j-1] < perm[j]: break
        j -= 1
    j -= 1
    # 2. swap perm[j] with minimum bigger than perm[j] in the suffix after j
    j2 = n-1
    while perm[j] > perm[j2]: j2 -= 1
    perm[j], perm[j2] = perm[j2], perm[j]  # the swap
    # 3. arrange suffix after j into ascending order
    j += 1
    j2 = n-1
    while j < j2:
        perm[j], perm[j2] = perm[j2], perm[j]
        j += 1
        j2 -= 1
    return True


def prevPerm( perm ):
    n = len( perm )
    j = n-1 # last index
    # 1. find shortest non-ascending suffix of length  >= 2
    while True:
        if j == 0: return False # no next permutation
        if perm[j-1] > perm[j]: break
        j -= 1
    j -= 1
    # 2. swap perm[j] with maximum smaller than perm[j] in the suffix after j
    j2 = n-1
    while perm[j] < perm[j2]: j2 -= 1
    perm[j], perm[j2] = perm[j2], perm[j]  # the swap
    # 3. arrange suffix after j into descending order
    j += 1
    j2 = n-1
    while j < j2:
        perm[j], perm[j2] = perm[j2], perm[j]
        j += 1
        j2 -= 1
    return True





def pr( x, a ):
    n = ("   "+str(x))[-3:]
    print( n +" {"+  " ".join(list(map(str, a))) + "}"  )

# --------------------------------------------------------------------------------
#                 M A I N
#  --------------------------------------------------------------------------------

x = 1
for k in range(30):
    print(x, sep = "  ",end = " ")
    x = (x*10+0) % 29

exit()

n = 5
p = [1,2,3,4,5]
p = [5,4,3,2,1]
while True:
    print(p)
    if not prevPerm(p): break
exit()
n = 9
subset = [6,7,8,9]
rank = 9*8*7*6 // (2*3*4) -1
while True:
    pr(  rank, subset )
    rank -= 1
    if not prevSubset( subset, n ): break

exit()

su = [4, 7]
n = 7
r = rankSubsetpr( su, n )
print( r )

su = [1,3,5,7,9]
n = 10
r = rankSubsetpr( su, n )
print( r )



r = 8
n = 5
k = 3
a = unrankSubset( r, n, k )
print( a )

exit()


su = [6,8,9,11]
n = 12
r = rankSubsetpr( su, n )
print( r )

su = [6,7,8,9,10]
n = 10
r = rankSubsetpr( su, n )
print( r )


exit()

chi = [0,0,0,0]
while True:
    print( chi )
    if not nextChi( chi ): break

subset = [1,2,3,4]
rank = 0
while True:
    #print( rank, subset )
    #print( rank, "", end = "" )
    pr(  rank, subset )
    rank += 1
    if not nextSubset( subset, 12 ): break




