#include <stdlib.h>
#include <stdio.h>

void sequence(int n, int max_age) {
    int i;

    long long mature = 0, died = 0, pop_new, pop_old = 1,
              *born = malloc((n + 1) * sizeof(long long)),
              *diff = malloc((n + 1) * sizeof(long long));

    printf("popu:");

    born[0] = 1;
    printf(" 1");

    for( i = 1; i < n; i++ ) {
        died = i < max_age ? 0 : born[i - max_age];
        born[i] = mature;
        mature = mature + born[i - 1] - died;
        printf(" %lld", pop_new = mature + born[i]);
        diff[i] = pop_new - pop_old;
        pop_old = pop_new;
    }

    puts("");

    printf("diff:  ");

    for( i = 1; i < n; i++ )
        printf(" %lld", diff[i]);

    puts("");

    free(born);
    free(diff);
}

int main(int argc, char *argv[]) {
    int max_age = 0, generations;

    if( argc < 2 ) {
        printf("Usage: %s GENERATIONS [ MAX_AGE ]\n", argv[0]);
        return 1;
    }

    max_age = generations = atoi(argv[1]);

    if( argc > 2 && (max_age = atoi(argv[2])) < 2 ) {
        puts("MAX_AGE should be at least 2.");
        return 1;
    }

    sequence(generations, max_age);

    return 0;
}
