The problem asks to find out n such that for given a, b, m
( (a + b√5)^n ) % m = 1
If no n is found then you have to output -1.
Observation:
Now lets find out the first k for which
((a + b√5)^k) % m = p, p is an integer.
Then ((a + b√5)^tk) % m = q, q is an integer.
We also can figure out that k < m & t < m. So we can traverse for the value of k. Then We can traverse again for the value of t such that q = 1. After that we have to print tk.
Code:
- cin >> T;
- while(T--){
- cin >> a >> b >> m;
- c = 1, d = 0;
- int period = 0;
- REP(i, m + 10){
- x = (a * c + 5ll * b * d) % m;
- y = (a * d + b * c) % m;
- c = x; d = y;
- if(d == 0){
- period = i + 1;
- break;
- }
- }
- LL tmp = c, fin = 0;
- e = 1;
- REP(i, m + 10){
- e *= tmp;
- e %= m;
- if(e == 1){
- fin = (i + 1) * period;
- break;
- }
- }
- if(fin == 0)cout << -1 << endl;
- else cout << fin << endl;
- }
No comments:
Post a Comment