Advertisement
Rochet2

THS

Sep 19th, 2015
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.03 KB | None | 0 0
  1. --[[
  2. http://www.ac-web.org/forums/showthread.php?212872-Stuck-on-a-coding-issue
  3. 2*nCr(V/2, K+1) where V = 2^N
  4. It's a close enough answer. Regarding the combinations, you can use stirling's approximation or a multiplicative formula - both result in O(ln(n)) time. I'm so glad you answered, good to meet another engineer on this forum.
  5. ]]
  6.  
  7. local N, K = 100000, 2
  8. -- The program below is this, but with modulo 100003:
  9. -- 2*nCr(V/2, K+1) where V = 2^N
  10.  
  11. local M = 100003 -- modulo
  12. local V = 2; -- vertices mod M or similar
  13. for i = 1, N-2 do V = (V*2)%M end -- this is not working properly .. for obvious reasons and for being tied to what K is
  14.  
  15. local n = V
  16. local m = K+1
  17.  
  18. local function pot(x, n)
  19.     if (n == 0) then return 1 end
  20.     if (n%2 == 1) then return (pot(x,n-1)*x)%M end
  21.     local t = pot(x,n//2)
  22.     return (t*t)%M
  23. end
  24.  
  25. local function inv(x)
  26.     return pot(x, M-2)
  27. end
  28.  
  29. local t = 1
  30. for i = 1, n do t = (t*i)%M end
  31. for i = 1, m do t = (t*inv(i))%M end
  32. for i = 1, n-m do t = (t*inv(i))%M end
  33.  
  34. print("Answer:", 2*t)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement