Hello Codeforces How to calculate pow(a,nCr) % p efficiently? Here 1 <= n <= 10^6, 1 <= r <= 10^6 and 1<= a <= 10^6
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3773 |
3 | Radewoosh | 3646 |
4 | ecnerwala | 3624 |
5 | jqdai0815 | 3620 |
5 | Benq | 3620 |
7 | orzdevinwang | 3612 |
8 | Geothermal | 3569 |
8 | cnnfls_csy | 3569 |
10 | Um_nik | 3396 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | Um_nik | 164 |
2 | cry | 160 |
2 | maomao90 | 160 |
4 | -is-this-fft- | 159 |
5 | atcoder_official | 158 |
5 | awoo | 158 |
7 | adamant | 155 |
8 | nor | 154 |
9 | maroonrk | 152 |
10 | Dominater069 | 149 |
Hello Codeforces How to calculate pow(a,nCr) % p efficiently? Here 1 <= n <= 10^6, 1 <= r <= 10^6 and 1<= a <= 10^6
Название |
---|
Sorry, i got it wrong
Hint: Use Legendre's formula and Fermat's little Theorem.
To find a^b %m when b is too large, we calculate a^(b%(m-1))%m this can be proceed using fermat little theorem.
To calculate nCr%(m-1) you can use chinese remainder theorem if m-1 is non prime and square free.
I prove a^b %m= a^(b%(m-1))%m in this way, but I am not sure whether it is correct or not.
Fermat's little theorem tells that a^(m-1) %m=1, and thus we can write b=x*(m-1)+y. Then, a^b %m=a^(x*(m-1)+y) %m= a^y * (a^(m-1))^x %m= (a^y %m) * (a^(m-1) %m)^x=a^y %m =a ^ (b%(m-1)) %m.
If this is correct, I really have never considered using Fermat's little theorem in this manner... My first reaction is to calculate the inverse of b when we need to compute a/b %m. Thank you so much for sharing such wonderful idea and extending my thought.
Can you elaborate more on how you calculte nCr%(m — 1) using chinese remainder theoreom ? Suppose that (m — 1) is non-prime.