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).
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | Dominater069 | 154 |
8 | awoo | 154 |
10 | luogu_official | 150 |
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).
Name |
---|
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.