I have big numbers K
, C[1]
, C[2]
, C[3]
etc. and I have to calculate b:
b = C[1]*C[2]+C[3]*C[4]+... (mod K)
Now I calculate the full sum and then make something like
b = SUM % K.
But this is not work when SUM becomes bigger then unsigned long limit, so I have to use something like
b = (C[1]*C[2] %K + C[3]*C[4] %K ) %K
But this is time-consuming. I've tried to use unsigned long long except unsigned long and this is time-consuming too. Is there any better way?
UPD:
C = (unsigned long long int *) malloc(N*sizeof(unsigned long long int));
unsigned long int i, j, l;
C[0] = 1;
for (i=1; i<=N; i++) {
C[i] = 0;
l = (unsigned long int) i/2;
for (j=0; j<l; j++) {
C[i] += C[j]*C[i-j-1];
C[i] = C[i] % K;
}
C[i] = C[i]*2;
C[i] = C[i] % K;
if (i - l*2 == 1) {
C[i] += C[l]*C[l];
}
C[i] = C[i] % K;
}