Apply modulus and still not affect series

RBX

In the zone
I found that I was getting poorer at programming, so joined Codechef and started working on an easy practice problem. Here it is - Golden Trees | CodeChef

I prepared the skeleton (maybe very un-optimized), but am confused when to apply modulus so as not to lose values because of data type limits and still not affect the series.

Code:
#include<stdio.h>
#include<malloc.h>


int main(void) {
    int *initTax, *slot1, *slot2, *K, *N, T;
    unsigned long int **tax;
    printf("Enter no. of Test cases ");
    scanf("%i", &T);
    
    initTax =     (int*) malloc(T * sizeof(int));
    slot1   =     (int*) malloc(T * sizeof(int));
    slot2   =     (int*) malloc(T * sizeof(int));
    K       =     (int*) malloc(T * sizeof(int));
    N       =     (int*) malloc(T * sizeof(int));
    tax     =     (unsigned long int**) malloc(T * sizeof(unsigned long int*));
    int i=0;
    for(i; i < T; ++i) {
        scanf("%d %d %d %d %d", &initTax[i], &slot1[i], &slot2[i], &K[i], &N[i]);
        tax[i] = (unsigned long int*) malloc (N[i] * sizeof(unsigned long int));
        tax[i][0] = initTax[i];
    }
    
    int j,k;
    unsigned long long sum=1;
    for(i=0; i<T; ++i) {
        k=slot1[i];
        for(j=1; j < N[i] && k > 0; j++) {
            tax[i][j] = tax[i][j-1] + 1;
            k--;
        }
        k=slot2[i];
        for(j; j < N[i] && k > 0; j++) {
            tax[i][j] = tax[i][j-1] * 2;
            k--;
        }
        for(j; j < N[i]; j++) {    
            for(k=K[i]; k >= 1; --k)
                sum *= tax[i][j-k];
            tax[i][j]=sum;
            sum = 1;
        }
    }

I'm not expecting exact solution, a hint would be good enough.
 
Last edited:
Top Bottom