3

UPDATE: C(n, k) means Binomial Coefficient

I'm dealing with a number theory problem. I've transformed the big problem into a simple problem: How to calculate C(n,k)%3, for which n<=10^15. There are about m ( <= 10 000 ) data-set needed to calculate within 1 second.

The way I solve it is using the Lucas' theorem. It's O(m log n). And it's fast enough to solve the problem. But I'm wondering if there is a better solution to this problem, and even to n <= 10^100 or m <= 1 000 000?

Thanks a lot!

abcdabcd987
  • 2,043
  • 2
  • 23
  • 34
  • Just FYI O(m log n) is considered pretty "quick" usually. – NominSim Dec 28 '12 at 14:35
  • For n=10^100, using Lucas' thm only requires log n = 100 * log_3 10. Even m=1 000 000 at the same time, it's fast enough. – Xiao Jia Dec 28 '12 at 14:43
  • 1
    possible duplicate of [Calculating Binomial Coefficient (nCk) for large n & k](http://stackoverflow.com/questions/3537360/calculating-binomial-coefficient-nck-for-large-n-k) – NominSim Dec 28 '12 at 14:45
  • I think this should be reopened because we don't have to calculate the binomial coefficient at all, we just have to compute the prime factors raised to a power modulo 3, and we know the prime factors of factorial like http://smallissimo.blogspot.fr/2011/07/evaluating-factorial-from-its-prime.html – aka.nice Dec 28 '12 at 17:36
  • you can use dynamic programming to calculate this. – amin k Dec 28 '12 at 19:35

0 Answers0