I have a function
f(x)=(1^1)*(2^2)*(3^3)*.....(x^x)
i have to calculate (f(x)/(f(x-r)*f(r)))modulo c
i can calculate f(x) and (f(x-r)*f(r)).
assume f(x) is a
and f(x-r)*f(r) is b.
c is some number that is very larger. ``
so i how can calculate (a/b)%c
Asked
Active
Viewed 702 times
-1

asif_mohammed
- 43
- 2
-
2In what language? Maybe it has a multi-precision library. – Barmar Nov 09 '14 at 05:36
-
You can at least cancel all factors from `r+1` thru `x-r`. – Nico Schertler Nov 09 '14 at 07:01
-
http://stackoverflow.com/questions/10118137/fast-n-choose-k-mod-p-for-large-n/10118336#10118336 – Ivaylo Strandjev Nov 21 '14 at 12:01
1 Answers
0
your f(x) is just ᴨ (PI cumulative multiplication) squared
- it is hard to write it in here so i will deifine g(x0,x1) instead
g(x0,x1)=x0*(x0+1)*(x0+2)*...*x1
- so:
f(x)=g(1,x)^2
computing
h(x,r,c)=f(x)/(f(x-r)*f(r))%c
- when you rewrite it to g() you get:
h(x,r,c)=((g(1,x)/(g(1,x-r)*g(1,r)))^2)%c
- now simplify (and lower magnitude) as much as you can
- so compute
sqr
as last (no need to have in sub-results) - get rid of duplicate therms
- there are two options get rid of
g(1,x-r)
org(1,r)
- choose what is better for you I would chose whichever is bigger
- so if
(x-r>r)
then: h(x,r,c)=(g(x-r+1,x)/g(1,r)^2)%c
- else:
h(x,r,c)=(g(r+1,x)/g(1,x-r)^2)%c
some math tweaks
- you should compute both therms a,b (from (a/b)%c) parallel
- when both can be divided by any of first few primes then divide them booth to keep the magnitude low
- something like::
- `if ((a&1==0)&&(b&1==0)) { a>>=1; b>>=1; } // dividing by prime = 2 ... this is usually enough
- but if your magnitudes are very big then may be you should add also some like 3,5,7,...
- but always measure the performance drop and stop if it drops too much
if
x
is big andr
is small- then compute
b
first - and while computing
a
- in addition to primes check also if the sub-result is divisible also by
b
- if it is then divide
a
byb
and add the result to the final division result
- then compute
some speed boost
- you can compute partial
a,b
- and start testing the divisibility only if booth have magnitudes bigger then some treshold
- also you can make the computation waiting for each other
- so if
a>1000000
then wait until theb
is also so big or whole - and do the same also in revers (if
b>100000
....) - the bigger treshold the better speed but you are limited by your integer implementation
- if using bigints then you should use treshold smaller then halve the bits of the base number ...
- you can compute partial
Hope I did not make some silly math mistake...

Spektre
- 49,595
- 11
- 110
- 380