modulo - How to use a very long integer to compute a value in C++? -


i need compute (n*(2n+1)*(n+1)*(3(n^2)+3n+1))/30 modulo m where: - 30 & m can non co-prime - n can have values 10^10 (too big long long int) - m can have value 10000

i have tried this:

long long int tp1; double k; k=n; k=k*((2*n)+1); k=k*(n+1); k=k*((3*n*n)+(3*n)-1); tp1=fmod(k/30,m); 

as noted molbdnilo, can calculations modulo number.

to expand idea:

  • (x * y) mod m same ((x mod m) * (y mod m)) mod m
  • (x + y) mod m same ((x mod m) + (y mod m)) mod m

however

  • (x / y) mod m not same ((x mod m) / (y mod m)) mod m

therefore, have calculate n*(2n+1)*(n+1)*(3(n^2)+3n+1) mod (30 * m), , divide 30.

p.s. assumed division operation means same in c++, "divide rounding down".


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -