• hdu 2842 Chinese Rings


    点击打开hdu2842

    思路: 矩阵快速幂

    分析:

    1 题目的意思是给定n个环,和一些规则要把所有的环全部拆下最少需要的步数

    2 题目规定如果要拆第n个环,那么第n-1个要挂着,n-2环要被拆下。那么我们设f(n)表示拆下前n个环的最少的步骤

       那么考虑第n个环的情况,第n-1个环必须要挂着,n-2环要拆下,那么这一步就要f(n-2),拆下第n个需要1步。然后只剩下第n-1个环,由于n-1环需要第n-2环挂着,所以我们需要把前n-2个环挂上去,所以需要f(n-2),剩下n-1个需要拆下需要f(n-1)。那么总的需要f(n) = f(n-2)+1+f(n-2)+f(n-1) => f(n) = 2*f(n)+f(n-1)+1

    3 接下来利用矩阵快速幂即可


    代码:

    /************************************************
     * By: chenguolin                               * 
     * Date: 2013-08-24                             *
     * Address: http://blog.csdn.net/chenguolinblog *
     ***********************************************/
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    
    typedef long long int64;
    const int MOD = 200907;
    const int N = 3;
    
    int n;
    struct Matrix{
        int64 mat[N][N];
        Matrix operator*(const Matrix& m)const{
            Matrix tmp;
            for(int i = 0 ; i < N ; i++){
                for(int j = 0 ; j < N ; j++){
                    tmp.mat[i][j] = 0;
                    for(int k = 0 ; k < N ; k++){
                        tmp.mat[i][j] += mat[i][k]*m.mat[k][j]%MOD;        
                        tmp.mat[i][j] %= MOD;
                    }
                }
            }
            return tmp;
        }  
    };
    
    int Pow(Matrix m){
        Matrix ans;
        if(n <= 1)
           return n;
        memset(ans.mat , 0 , sizeof(ans.mat));
        for(int i = 0 ; i < N ; i++)
            ans.mat[i][i] = 1;
        n--;
        while(n){
            if(n&1)
                ans = ans*m;
            n >>= 1;
            m = m*m;
        }
        int sum = 0;
        sum += ans.mat[0][0]%MOD;
        sum %= MOD;
        sum += ans.mat[0][2]%MOD;
        return sum%MOD;
    }
    
    int main(){
        Matrix m;
        memset(m.mat , 0 , sizeof(m.mat));
        m.mat[0][0] = 1 , m.mat[0][1] = 2 , m.mat[0][2] = 1; 
        m.mat[1][0] = 1 , m.mat[2][2] = 1; 
        while(scanf("%d" , &n) && n)
             printf("%d
    " , Pow(m));
        return 0;
    }
    
    


  • 相关阅读:
    关于VS2008单元测试中加载配置文件的问题
    面向对象控与python内存泄漏
    热酷,新的领域,新的发展
    [思想火花]:函数命名及参数
    使用AuthToken架构保护用户帐号验证Cookie的安全性
    竟然遇到取FormAuthentication的值取不出来的情况
    新头衔:热酷高级python软件工程师,你问我去热酷干嘛?
    浅谈滚服游戏如果实现一键合服
    基础
    简介
  • 原文地址:https://www.cnblogs.com/riskyer/p/3279658.html
Copyright © 2020-2023  润新知