3

Question is about the modulo operator on very large numbers.

For example consider a question where the total number of permutations are to be calculated. Consider a number of 90 digits with each of the 9 numbers (1 to 9) repeating 10 times so 90!/(10!)^9) is to be calculated

After reading many answers on StackOverflow I used logarithms to do it.

Now consider the log value to be 1923.32877864.

Now my question is how can I display the answer (i.e. 10 ^ log10(value) ) modulo of "m"?

And is this the best method for calculating the possible number of permutations?

Edit Got the solution :)

Thanks to duedl0r.

Did it the way you specified using Modular Multiplicative Inverse.Thanks :)

2 Answers2

1

I'm not sure whether this is actually possible and correct, but let me summarize my comments and extend the answer from Miky Dinescu.

As Miky already wrote:

a × b ≣m am × bm

You can use this in your equality:

90! / 10!^9 ≣m x

Calculate each term:

90!m / 10!^9mm x

Then find out your multiplicative inverse from 10!^9m. Then multiplicate the inverse with 90!m.


update This seems to be correct (at least for this case :)). I checked with wolfram:

(90!/10!^9) mod (10^9+7) = 998551163

This leads to the same result:

90! mod (10^9+7) = 749079870
10!^9 mod (10^9+7) = 220052161

do the inverse:

(220052161 * x) mod(10^9+7) = 1 = 23963055

then:

(749079870*23963055) mod (10^9+7) = 998551163

No proof, but some evidence that it might work :)

duedl0r
  • 9,289
  • 3
  • 30
  • 45
  • Got the solution :) Thanks to duedl0r. Did it the way you specified using Modular Multiplicative Inverse Thanks :) – anudeep2011 Feb 03 '12 at 17:57
0

I would argue that the way to compute the total number of permutations modulo m, where m is an arbitrary integer (usually chosen to be a large prime number) is to use the following property:

 (a * b) % m = ((a % m) * (b % m)) % m

Considering that the total number of permutations of N is N! = 1 * 2 * 3 * .. * N, if you need to compute N! % m, you can essentially apply the property above for multiplication modulo m, and you have:

 ((((1 * (2 % m)) % m) * (3 % m)) % m) * .. 

EDIT

In order to compute the 90! / (10! ^ 9) value you could simplify the factors and then use multiplication modulo m to compute the final result modulo m.

Here's what I'm thinking:

90! = 10! * (11 * 12 * .. * 20) * (21 * 22 * .. * 30) * .. * (81 * 82 * .. * 90)

You can then rewrite the original expression as:

(10! * (11 * 12 * .. * 20) * (21 * 22 * .. * 30) * .. * (81 * 82 * .. * 90)) / (10! * 10! * ... * 10!)

At the numerator, you have a product of 9 factors - considering each expression in parenthesis a factor. The same is true for the denominator (you have 9 factors, each equal to 10!).

The first factor at the denominator is trivial to simplify. After that you still have 8 pairs that need simplification.

So, you can factor each term of the products and simplify the denominator away. For example:

11 * 12 * 13 * 14 * 15 * 16 * 17 * 18 * 19 * 20 <=> 11 * 2 * 2 * 3 * 13 * 2 * 7 * 3 * 5 * 2 * 2 * 2 * 2 * 17 * 2 * 9 * 2 * 2 * 5

The denominator will always be: 2 * 3 * 2 * 2 * 5 * 2 * 3 * 7 * 2 * 2 * 2 * 2 * 3 * 3 * 2 * 5

After the simplification the second pair reduces to : 2 * 2 * 11 * 13 * 17 * 19

The same can be applied to each subsequent pair and you will end up with a simple product that can be computed modulo m using the formula above.

Of course, efficiently implementing the algorithm to perform the simplification will be tricky so ultimately there has to be a better way that eludes me now.

Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
  • How do you calculate `90!/10!^9` with this? `(90! % m) / 10!^9` doesn't look right.. or even if you take `10^1923.32` you run into problems, since 1923.32 is not an even number.. – duedl0r Feb 03 '12 at 15:26
  • exactly as duedl0r told. that is the problem i m facing .how can i work with divisions .. if done ,mostly its resulting 0, as numerator < denominator . – anudeep2011 Feb 03 '12 at 15:32
  • I think you can multiply by 10!^9. But it's not mentioned in this solution.. something like this: 90!%m = x*(10!^9%m) %m. Then search for an `x` :) – duedl0r Feb 03 '12 at 15:37
  • maybe you can even use euler to get the inverse.. http://en.wikipedia.org/wiki/Modular_multiplicative_inverse – duedl0r Feb 03 '12 at 15:42
  • hmm, I guess I missed out an important aspect of your question @anudeep2011. I remember there being a method for computing modular exponentiaion with non-integer exponents but I can't remember exactly how it goes. Modular exponentiation with integer exponents is easy.. – Mike Dinescu Feb 03 '12 at 16:01