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 );
    }
  • 相关阅读:
    php生成excel
    gearmand
    开启Nginx的目录文件列表功能
    zend框架学习
    引用方法形成树
    智能指针实现
    图文例解C++类的多重继承与虚拟继承
    CC++定位崩溃代码行的方法
    C++函数重定义、重载、重写
    勤奋吧,一天一点,努力提高基本技能。
  • 原文地址:https://www.cnblogs.com/jrfr/p/13518000.html
Copyright © 2011-2022 走看看