zoukankan      html  css  js  c++  java
  • Codeforces

    【题目大意】
    已知f1=x,f2=y,i(i2)fi=fi1+fi+1.现给你n,x,y要求fnmod(109+7)


    【数据范围】
    1n2109,|x|,|y|109


    【分析】
    fi=fi1+fi+1,即fi=fi1fi2,又由于n太大,普通的计算方法肯定不行,所以我想到了矩阵快速幂来做,参照斐波拉契数列的矩阵,此题矩阵如下:

    [Fn+1Fn]=[1110][FnFn1]

    可转换如下:

    [FnFn1]=[1110]n2[F2F1](n2)

    即最后答案Fn=A[0][0]F[2]+A[0][1]F[1],F2=y,F1=x
    注意最后答案MOD(109+7)


    【代码】

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<cstdlib>
    #include<vector>
    #include<algorithm>
    using namespace std;
    typedef long long LL;
    typedef vector<LL> vec;
    typedef vector<vec> mat;
    const LL MOD = 1000000007LL;
    mat mul(mat &A, mat &B) {
        mat C(A.size(), vec(B[0].size()));
        for(int i = 0; i < A.size(); i++)
            for(int k = 0; k < B.size(); k++)
                for(int j = 0; j < B[0].size(); j++)
                    C[i][j] = ((C[i][j] + A[i][k] * B[k][j]) % MOD + MOD) % MOD;
        return C;
    }
    mat pow(mat A, LL n) {
        mat B(A.size(), vec(A.size()));
        for(int i = 0; i < A.size(); i++)B[i][i] = 1;
        while(n > 0) {
            if(n & 1)B = mul(B, A);
            n >>= 1;
            A = mul(A, A);
        }
        return B;
    }
    void solve(LL x, LL y, LL n) {
        mat A(2, vec(2));
        A[0][0] = 1, A[0][1] = -1;
        A[1][0] = 1, A[1][1] = 0;
        A = pow(A, n - 2);
        printf("%lld
    ", ((A[0][0] * y % MOD + A[0][1] * x % MOD)%MOD + MOD)% MOD);
    }
    int main() {
        LL x, y, n;
        scanf("%lld%lld%lld", &x, &y, &n);
        if(n == 1)printf("%lld
    ", (x + MOD) % MOD);
        else if(n == 2)printf("%lld
    ", (y + MOD) % MOD);
        else solve(x, y, n);
        return 0;
    }
  • 相关阅读:
    Mysql::Error: Commands out of sync; you can't run this command now: SHOW TABLES解决方案
    mysql安装失败Error Nr. 1045
    TermServDevices 报错【远程服务使用者必读】
    数据库出现“提取逻辑页失败”
    Ruby学习——数据库操作
    VS2008 安装失败
    Ubuntu Server 安装图解
    C#的Enum——枚举
    SQLServer2005数据库被置为“可疑”
    ROR之include、extend、send对module、class进行扩展
  • 原文地址:https://www.cnblogs.com/TRDD/p/9813521.html
Copyright © 2011-2022 走看看