# 51nod 分解

UsedToBe (命题人)

Input

Output

Input示例
2
Output示例
9

（1+sqrt(2))^n 可以化成 a + b*sqrt(2)的形式。

a b n
1 1 1
3 2 2
7 5 3
17 12 4
…..

bn = an-1 + bn-1

| 1 2 | * | an-1 | = | an |
| 1 1 | | bn-1 | | bn |

#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<ctime>
#include<string>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#include<set>
#include<map>
#include<cstdio>
#include<limits.h>
#define MOD 1000000007
#define fir first
#define sec second
#define fin freopen("/home/ostreambaba/文档/input.txt", "r", stdin)
#define fout freopen("/home/ostreambaba/文档/output.txt", "w", stdout)
#define mes(x, m) memset(x, m, sizeof(x))
#define Pii pair<int, int>
#define Pll pair<ll, ll>
#define INF 1e9+7
#define Pi 4.0*atan(1.0)

#define lowbit(x) (x&-x)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-7;
const int maxn = 101;
using namespace std;
//#define time
struct Matrix
{
ull mat[2][2];
void output(){
for(int i = 0; i < 2; ++i){
for(int j = 0; j < 2; ++j){
cout << mat[i][j] << " ";
}
cout << endl;
}
}
void clear(){
mes(mat, 0);
}
void init(){
mes(mat, 0);
for(int i = 0; i < 2; ++i){
mat[i][i] = 1;
}
}
Matrix operator *(const Matrix &b) const{
Matrix tmp;
tmp.clear();
for(int i = 0; i < 2; ++i){
for(int j = 0; j < 2; ++j){
for(int k = 0; k < 2; ++k){
tmp.mat[i][j] = (tmp.mat[i][j] + mat[i][k]*b.mat[k][j])%MOD;
}
}
}
return tmp;
}
};

Matrix fast_mod(ull n, Matrix &base){
Matrix res;
res.init();
while(n){
if(n&1){
res = res*base;
}
base = base*base;
n >>= 1;
}
return res;
}

int main()
{
ull n, m;
cin >> n;
Matrix base = {
1, 2,
1, 1
};
base = fast_mod(n-1, base);
Matrix p = {
1, 0,
1, 0
};
p = base*p;
if(n&1){
m = p.mat[1][0]*p.mat[1][0]*2%MOD;
}
else{
m = p.mat[0][0]*p.mat[0][0]%MOD;
}
cout << m << endl;
return 0;
}

#### HDU GT and numbers (整数的唯一分解定理)

2015-10-17 23:18:54

#### 51nod 1383&1048 整数分解为2的幂 [递推]【数学】

2017-03-02 09:28:49

#### 51nodProblem_1836.in

2016年12月09日 24KB 下载

#### [51nod1383&1048]整数分解为2的幂

2017-01-05 12:28:10

#### 51NOD 1537 分解

2016-09-01 14:47:20

#### 51nod - 1573 分解 - 矩阵快速幂

2016-08-31 14:01:10

#### [51nod 1537]分解

2016-09-02 21:43:42

#### 51nod 1537 分解

2016-12-07 20:21:12

#### 51nod 1400 序列分解

2016-11-17 21:19:24

#### 51nod 1537 分解 (矩阵快速幂)

2016-09-30 00:38:40