1≤n≤2000000000,
1≤m≤1000000010
数据的范围
* f1 = 1 , f2 = 1 , f3 = 2 , f4 = 3 ....
f[i] = f[i - 1] + f[i - 2]
然后 f[i] = f[i - 1] + f[i - 2] + f[i - 3] + ... f[1]
普通的做法
但是此题的范围比较大 所以需要用矩阵乘法来做
fn = fn-1 + fn-2
Sn - Sn-1 = fn = fn-1 + fn-2 = sn-1 - sn-2 + sn-2 - sn-3 = sn-1 - sn-3
sn - sn-1 = sn-1 - sn-3
sn = 2sn-1 - sn-3
求sn ? sn = sn-1 * A 即 sn-1 * A = sn
A = ?
s1 * An-1 = sn
令 FN = {fn , fn+1 , sn};
FN+1 = {fn+1 , fn+2, sn+1};
FN * A = FN+1 所以 A = {0 1 0
1 1 1
0 0 1
};
*
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int n , m;
void mul(int a[] , int b[] , int c[][3])
{
int temp[3] = {0};
for(int i = 0 ; i < 3 ; i ++)
for(int j = 0 ; j < 3 ; j ++)
temp[i] = (temp[i] + (long long) b[j] * c[j][i]) % m;
memcpy(a , temp , sizeof temp);
}
void mul(int a[][3] , int b[][3] ,int c[][3])
{
int temp[3][3] = {0};
for(int i = 0 ; i < 3 ; i ++)
for(int j = 0 ; j < 3 ; j ++)
for(int k = 0 ; k < 3 ; k ++)
temp[i][j] = (temp[i][j] + (long long) b[i][k] * c[k][j]) % m;
memcpy(a , temp , sizeof temp);
}
int main()
{
cin >> n >> m;
int a[][3] = { {0 , 1 , 0},
{1 , 1 , 1},
{0 , 0 , 1}
};
int f[] = {1 , 1 , 1};
n --;
while (n){
if(n & 1) mul(f , f , a);
n >>= 1;
mul(a , a , a);
}
cout << f[2] << endl;
return 0;
}