快速幂需要注意一个地方,就是别超了数字的范围,及时longlong也很容易爆炸,及时取余才是王道
http://hihocoder.com/problemset/problem/1143
#include<iostream>
using namespace std;
long long di[4] ;
void solve(long long a[4], long long b[4], long long *cc)
{
long long aa[4] = {a[0],a[1],a[2],a[3]};
long long bb[4] = { b[0], b[1], b[2], b[3] };
cc[0] = (aa[0] * bb[0] + aa[1] * bb[2]) % 19999997;//重点啊~~~~
cc[1] = (aa[0] * bb[1] + aa[1] * bb[3]) % 19999997;
cc[2] = (aa[2] * bb[0] + aa[3] * bb[2]) % 19999997;
cc[3] = (aa[2] * bb[1] + aa[3] * bb[3]) % 19999997;
int i = 0;
}
void ji(long long n, long long *answer)
{
while (n)
{
if (n & 1)
{
solve(answer, di, answer);
}
solve(di, di, di);
n = n >> 1;
}
}
int main()
{
long long n;
while (cin >> n){
di[0] = 0;
di[1] = 1;
di[2] = 1;
di[3] = 1;
long long answer[4] = { 1, 0, 0, 1 };
ji(n, answer);
cout << answer[3] << endl;
}
return 0;
}