Hi everyone!
Some days ago, I confronted to a hard problem and I could not solve it. Can you help me?
The problem was: Given a and n(1 <= a, n <= 1000). And b = (a ^ a) ^ (a ^ a). And return b mod n.
№ | Пользователь | Рейтинг |
---|---|---|
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 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
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 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
Hi everyone!
Some days ago, I confronted to a hard problem and I could not solve it. Can you help me?
The problem was: Given a and n(1 <= a, n <= 1000). And b = (a ^ a) ^ (a ^ a). And return b mod n.
Название |
---|
Auto comment: topic has been updated by VIIIIIIVX (previous revision, new revision, compare).
Use a^phi(n) is 1 mod n. Find a^a mod n, and a^a mod phi(n).
This is valid only if a and n are co — prime,and he has not mentioned so in the blog
True that. If that's not true, then that's some real trouble. Maybe this helps in that case.
Incase a, n are co-prime, I guess this will work.
You can use Euler's Theorem for xy % n even when gcd(x, n) ≠ 1. This requires that the residue used in the exponentiation is equal or bigger than the highest power of the prime powers shared by x and n. So in such case, find y % φ(n) until the value is the smallest value greater or equal to the highest power of the prime factors shared by x and n.
Having said this, in above case the highest shared power is ≤ 10. So calculations then can fit in long long.
A simple way is to compute the values 1, a, a2, a3, ... modulo n until you find a loop. Due to non-coprimeness issues, it may not end in 1, so it has the so-called ρ structure (a tail and a cycle). Still, we know the start of the cycle and its period, so we can reduce the exponent modulo this period.
So now we have to compute the exponent . This can be done straightforwardly or using fast exponentiation.
Oh yes yes, thans a lot!