Huge Fibonacci Numbers!
The main idea: Fibonacci series F[n], give you a,b,n, beg f[a^b]%n.
Idea: Number theory problem. F[a^b]%n is a cycle, we can find out this cycle will be simplified into f[(a% cycle ) ^b]% cycle by the use of the division of the power to take the model.
Note with unsigned long long (seemingly a long long twice times), or will overflow, and learned a trick ...
Do not know which bug, has been changed the wrong, has been, and later picked up others and their own code of the same change on the right,,,
#include <iostream>//uva#include<cstdio>using namespace Std;typedef unsigned long long ull;//unsigned long The range of long is a long long of twice times the const int N = 1100*1100;int f[n];int pow_mod (ull a,ull n,ull m)//Division method, power modulo { if (n==0) return 1; ull x = Pow_mod (a,n/2,m); ull ans = (ull) x * x% m; if (n%2==1) ans = ans*a%m; return (int) ans; int main () {int t,n;ull a,b;cin >> t;while (t--) {cin >> a >> b >> n;f[0] = 0, f[1] = 1%n;int ka se;for (int i = 2; I <= n*n+100; i++) {f[i] = (f[i-1]+f[i-2])% n;if (f[i]==f[1] && f[i-1]==f[0]) {Kase = i- 1;break;}} int ans = Pow_mod (a%kase, B, (ull) kase); cout << F[ans] << Endl;} return 0;}
UVA 11582-colossal Fibonacci numbers! (number theory) (power modulo by divide and conquer)