Loading

51Nod - 1537 分解

 问(1+sqrt(2)) ^n  能否分解成 sqrt(m) +sqrt(m-1)的形式 
如果可以 输出 m%1e9+7 否则 输出no
 
 
打表找规律。
得到 ai = 6 * a[i - 1] - a[i - 2] - 2
就硬找呗??
然后矩阵快速幂。注意负数取模的细节
 
值得一提的是,可以证明

 

 

 
 
#pragma warning(disable:4996)

#include<iostream>
#include<algorithm>
#include<bitset>
#include<tuple>
#include<unordered_map>
#include<fstream>
#include<iomanip>
#include<string>
#include<cmath>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<stack>
#include<sstream>
#include<cstdio>
#include<ctime>
#include<cstdlib>
#define pb push_back
#define INF 0x3f3f3f3f
#define inf 0x7FFFFFFF
#define moD 1000000003
#define pii pair<int,string>
#define eps 1e-8
#define equals(a,b) (fabs(a-b)<eps)
#define bug puts("bug")
#define re  register
#define fi first
#define se second
typedef  long long ll;
typedef unsigned long long ull;
const ll MOD = 1e9 + 7;
const int maxn = 1e6 + 5;
const double Inf = 10000.0;
const double PI = acos(-1.0);
using namespace std;


struct Mat {
    ll m[3][3];
};

Mat E;
Mat P;

void init() {
    P.m[0][0] = 6;
    P.m[0][1] = -1;
    P.m[0][2] = 1;
    P.m[1][0] = 1;
    P.m[2][2] = 1;
    for (int i = 0; i < 3; i++) E.m[i][i] = 1;
}

Mat mul(const Mat& a, const Mat& b) {
    Mat c;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            c.m[i][j] = 0;
            for (int k = 0; k < 3; k++) c.m[i][j] = (c.m[i][j] + (a.m[i][k] * b.m[k][j]) % MOD) % MOD;
        }
    }
    return c;
}

Mat quickPower(Mat a, ll b) {
    Mat ans = E;
    Mat base = a;
    while (b) {
        if (b & 1) ans = mul(ans, base);
        base = mul(base, base);
        b >>= 1;
    }
    return ans;
}

int main() {
    ll n;
    scanf("%lld", &n);
    init();
    if (!n) {
        puts("1");
        return 0;
    }
    if (n == 1) {
        puts("2");
        return 0;
    }
    else if (n == 2) {
        puts("9");
        return 0;
    }
    P = quickPower(P, n - 2);
    ll res = (((P.m[0][0] * 9) % MOD + P.m[0][1] * 2) % MOD + (-2 * P.m[0][2] % MOD + MOD) + MOD) % MOD;
    printf("%lld", res);
}

 

posted @ 2020-07-27 10:09  MQFLLY  阅读(145)  评论(0编辑  收藏  举报