• hihocoder #1143 : 骨牌覆盖问题·一


    http://hihocoder.com/problemset/problem/1143 
    
    斐波那契数列,快速矩阵幂解法
    
    #include <cstdio>
    #include <cstring>
    #include <vector>
    
    using namespace std;
    const int N = 19999997;
    
    void copy(long long a[2][2],long long b[2][2])
    {
        for(int i = 0; i < 2; i++)
            for(int j = 0; j < 2; j++)
                a[i][j] = b[i][j];
    
    }
    
    void calc(long long a[2][2],long long b[2][2])
    {
        long long c[2][2]={0};
        c[0][0] = (a[0][0]*b[0][0]+a[0][1]*b[1][0])%N;
        c[0][1] = (a[0][0]*b[0][1]+a[0][1]*b[1][1])%N;
        c[1][0] = (a[1][0]*b[0][0]+a[1][1]*b[1][0])%N;
        c[1][1] = (a[1][0]*b[0][1]+a[1][1]*b[1][1])%N;
        copy(a,c);
    }
    
    int main()
    {
        int n;
        while(scanf("%d",&n)!=EOF)
        {
            long long c[2][2]={0,1,1,1};
            long long res[2] = {1,1};
            int flag = 0;
            for(int k = 0; (1<<k) <= n;k++)
            {
                if(n & (1<<k))
                {
                    long long u = (res[0]*c[0][0]+res[1]*c[0][1])%N;
                    long long v = (res[0]*c[1][0]+res[1]*c[1][1])%N;
                    res[0] = u;
                    res[1] = v;
                }
                calc(c,c);
            }
            printf("%lld
    ",res[0]);
        }
        return 0;
    }
    
     
  • 相关阅读:
    oracle lengthb
    layui-rp
    1709052基于框架新建 子项目
    echar 常用单词
    Leetcode 481.神奇字符串
    Leetcode 480.滑动窗口中位数
    Leetcode 479.最大回文数乘积
    Leetcode 477.汉明距离总和
    Leetcode 476.数字的补数
    Leetcode 475.供暖气
  • 原文地址:https://www.cnblogs.com/zendu/p/4981011.html
Copyright © 2020-2023  润新知