zoukankan      html  css  js  c++  java
  • Codeforces 450B 矩阵快速幂

    xg

    题意

      f1为x,f2为y,fi=fi-1 + fi+1,求fn为多少(n=2e9)

    思路

      这题不用矩阵快速幂也可。

      换算式子为fi = fi-1 - fi-2。

      则易看出令f1 = {x,y},矩阵A为{{0,-1},{1,1}};

      答案即为f1 * An-1,因为存在负数,负数取模为(a%mod+mod)%mod

    #include <iostream>
    #include <cstdio>
    #include<string>
    #include<cstring>
    using namespace std;
    typedef long long ll;
    
    const int maxn = 2e5+10;
    const int mod = 1e9+7;
    int n;
    void mul(ll f[2] ,ll a[2][2])
    {
        ll c[2];
        memset(c,0,sizeof(c));
        for(int j = 0;j < 2; ++j)
            for(int k = 0;k < 2 ;++k)
                c[j] = ((c[j] + ((f[k] * a[k][j])%mod+mod)%mod) % mod+mod)%mod;
        memcpy(f,c,sizeof(c));
    } 
    void mulself(ll a[2][2])
    {
        ll c[2][2];
        memset(c,0,sizeof(c));
        for(int i = 0;i < 2;++i)
            for(int j = 0;j < 2; ++j)
                for(int k = 0;k < 2 ;++k)
                    c[i][j] = ((c[i][j] + ((a[i][k] * a[k][j])%mod+mod)%mod) % mod+mod)%mod;
        memcpy(a,c,sizeof(c));
    } 
    int main()
    {
        //freopen("input.txt", "r", stdin);
        int x,y,n;
        scanf("%d%d%d",&x,&y,&n);
        ll f[2] = {x,y};
        ll a[2][2] = {{0,-1},{1,1}};
        n--;
        for(;n;n>>=1){
            //cout<<"Ssss"<<endl;
            if(n&1){
                mul(f,a);
            }
            mulself(a);
        }
        printf("%d
    ",(f[0]%mod+mod)%mod );
    }
  • 相关阅读:
    货币
    沙漏
    秋季学习总结
    三个老师
    介绍自己
    redis 的部分配置
    第二次博客作业
    第一次阅读作业
    shell_通配符
    shell_/dev/null,>和&
  • 原文地址:https://www.cnblogs.com/jrfr/p/13518000.html
Copyright © 2011-2022 走看看