• 矩阵经典题目六:poj 3070 Fibonacci


    http://poj.org/problem?id=3070





    按已构造好的矩阵,那么该矩阵的n次方的右上角的数便是f[n]。


    #include <stdio.h>
    #include <iostream>
    #include <map>
    #include <set>
    #include <list>
    #include <stack>
    #include <vector>
    #include <math.h>
    #include <string.h>
    #include <queue>
    #include <string>
    #include <stdlib.h>
    #include <algorithm>
    #define LL long long
    #define _LL __int64
    #define eps 1e-12
    #define PI acos(-1.0)
    #define C 240
    #define S 20
    using namespace std;
    
    const int maxn = 110;
    
    struct matrix
    {
    	int mat[3][3];
    	void init()
    	{
    		memset(mat,0,sizeof(mat));
    		mat[1][1] = mat[2][2] = 1;
    	}
    }a;
    
    matrix mul(matrix a, matrix b)
    {
    	matrix res;
    	memset(res.mat,0,sizeof(res.mat));
    	for(int i = 1; i <= 2; i++)
    	{
    		for(int k = 1; k <= 2; k++)
    		{
    			if(a.mat[i][k] == 0) continue;
    			for(int j = 1; j <= 2; j++)
    			{
    				int t = a.mat[i][k]*b.mat[k][j];
    				res.mat[i][j] = (res.mat[i][j] + t)%10000;
    			}
    		}
    	}
    	return res;
    }
    
    matrix pow(matrix a, int n)
    {
    	matrix res;
    	res.init();
    
    	while(n)
    	{
    		if(n&1)
    			res = mul(res,a);
    		a = mul(a,a);
    		n >>= 1;
    	}
    	return res;
    }
    
    int main()
    {
    	int n;
    	while(~scanf("%d",&n))
    	{
    		if(n == -1) break;
    		a.mat[1][1] = a.mat[1][2] = a.mat[2][1] = 1;
    		a.mat[2][2] = 0;
    
    		matrix ans = pow(a,n);
    
    		printf("%d
    ",ans.mat[1][2]);
    	}
    	return 0;
    }
    


  • 相关阅读:
    mcstructs使用CMake生成Makefile文件
    打印man手册为pdf文件
    ubuntu安装wine之后进不了系统
    如何知道自己梦呓的内容
    Opencv step by step
    Opencv step by step
    树莓派实现只有一个按键的播放器
    virtualbox 打不开ubuntu解决
    wifi使用的一些误区
    Opencv step by step
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4183357.html
Copyright © 2020-2023  润新知