How to calculate: a ^ n mod BASE with n is a big integer, a and n are not coprimes? I have a formula: a ^ n mod BASE = a ^ (n % phi(BASE) + phi(BASE)) mod BASE. Is this correct? Can somebody prove it if correct?
Thanks. (Sorry for my bad English).
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 156 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
How to calculate: a ^ n mod BASE with n is a big integer, a and n are not coprimes? I have a formula: a ^ n mod BASE = a ^ (n % phi(BASE) + phi(BASE)) mod BASE. Is this correct? Can somebody prove it if correct?
Thanks. (Sorry for my bad English).
Название |
---|
Suppose you need 2^0 mod 4. The answer is 1 and you'll find 2^phi(4)=0
Thanks you for your reply, so, How to calculate: a ^ n mod BASE with n is a big integer, a and n are not coprimes? (Sorry for my bad English).
probably you mean a and base are not coprimes.
I think your formula should work for n >= phi(base)
So you may for each level calculate value with appropriate modulo and also check whether it's more than those module
thank you :) I will try (Sorry for my bad English)
You're welcome
BTW, Your English (It's not me who may judge if it is really bad) bothers me much less than (sorry for my bad English) at the end of each post:)
The function for computing what you want is the following (try to understand it as an exercise)
In the case where
n
is a big integer, you only need to convert it to binary and the apply the function above. I hope this can help you.NOTE: In the case that you don't understand the above function, just ask me
Just wondering — is it safe to assume, that ans * 1ll will happen first and get ans converted into long long? Is it defined behavior that compiler must multiply left to right one by one?
Arithmetic operators with same precedence are always evaluated from left to right.
But in the expression
(3 / 1) + (4 / 2)
operators/
may be called in any order.