Separate permutations of a string modulo simple

I recently thought about the following problem, and I am very surprised that no one seemed to ask this question:

For a string, how many different permutations of this exist, modulo 1000000007?

I know the formula formulawhere Nis the length of the string, and A1A2AKis the count of each character (given the alphabet size <img src = "/img/8448f6036f8b3c1845222e9d41e42efe.gif" alt = "K"> ). Thus, the string toffeewould have 180various permutations.

But this no longer works when it Ncan be really big (say 100,000), because the calculation Nfactorialgoes beyond the long long int, and using BigIntegers will be too slow. Is there a way to calculate this, for example, Nor Nlogntime?

If I pre-processed the factorials from 1to N, and my "strings" came in the form of an array of length K, where each element contains a counter for each letter, could it be calculated at Kor Klogktime?

Would thank for any help on this :)

+4
source share
4 answers

, p = 10^9 + 7 - . , :

n! / (a1!*...*ak!) = 

n! * a1!^(p - 2) * ... * ak!^(p - 2) (mod p)

mod p - ( ).

O(k log p + n), O(k) , - O(log p), n! .

, .

+6

, , . , "" . , , , .

, , .

+2

N ( , , , - Sieve of Eratosthenes), N! .

, , .

, ( ), , , .

0

.. . .

modulo 1000000007 ( ), . mod - O (N + K * log (mod)).

0

Source: https://habr.com/ru/post/1608451/


All Articles