I have large 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 total amount and then do something like
b = SUM % K.
But this does not work when the SUM gets larger than the unsigned long limit, so I should use something like
b = (C[1]*C[2] %K + C[3]*C[4] %K ) %K
But it is time consuming. I tried to use unsigned long long besides unsigned long, and this also takes a lot of time. Is there a 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[ij-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; }
Ximik source share