import math


def Distribute(nEl, size, rank):
    """
    Distribute 'nEl' elements among 'size' containers, return the list of
    elements in container 'rank'
    nEl is the number of elements to distribute
    size is total number of process
    rank is the ID of this process
    returns list of indices for this process to handle
    """
    if size < 1:
        raise Exception('size cannot be zero')
    if size == 1:
        return range(nEl)
    else:
        if nEl < size:
            if rank < nEl:
                return [rank]
            else:
                return []
        else:
            # minimum number of elements given to any process
            nProcEl = int(math.floor(nEl/size))
            # number of remaining elments to split up
            remaining = nEl % size

            if rank < remaining:
                # one extra element in this bin
                nProcEl += 1
                startEl = nProcEl*rank
            else:
                startEl = (nProcEl+1)*remaining + nProcEl*(rank-remaining)
            return range(startEl, startEl+nProcEl)
    raise Exception('Should not get here')