def get_job_and_subjob_indices(n_jobs, n_tasks, task_index):
    """
    For example, if there are 170 tasks for working on 50 jobs, than each job will be divided to 3-4 tasks.
    Since 170 % 50 = 20, the 20 first jobs will receive 4 tasks and the last 30 jobs will receive only 3 tasks.
    In total, the first 80 tasks will be dedicated to jobs with 4 tasks each, and the 90 last tasks will be
    dedicated to jobs with 3 tasks each. Hence, tasks 0-3 will go to job 0, tasks 4-7 will go to job 1, and so on;
    tasks 80-82 will go to job 21, tasks 83-85 will job to job 22, and so on.  
    """
    assert n_tasks >= n_jobs
    n_tasks_in_unprivileged_jobs = n_tasks // n_jobs
    n_tasks_in_privileged_jobs = n_tasks_in_unprivileged_jobs + 1
    n_privileged_jobs = n_tasks % n_jobs
    n_tasks_of_privileged_jobs = n_tasks_in_privileged_jobs * n_privileged_jobs
    if task_index < n_tasks_of_privileged_jobs:
        job_index = task_index // n_tasks_in_privileged_jobs
        index_within_job = task_index % n_tasks_in_privileged_jobs
        n_tasks_in_job = n_tasks_in_privileged_jobs
    else:
        task_index_in_unprivileged_group = (task_index -
            n_tasks_of_privileged_jobs)
        job_index = (n_privileged_jobs + task_index_in_unprivileged_group //
            n_tasks_in_unprivileged_jobs)
        index_within_job = (task_index_in_unprivileged_group %
            n_tasks_in_unprivileged_jobs)
        n_tasks_in_job = n_tasks_in_unprivileged_jobs
    return job_index, index_within_job, n_tasks_in_job
