POJ 3070 矩阵mob

.

矩阵高速幂想法与快速幂相同

#include<iostream>
#include<cstdio>
#include<cstring>
#define MOD 10000
using namespace std;
struct matrix {
    int mat[2][2];
    matrix() {      //构造函数与结构体同名 若写作init()函数 需调用
        memset(mat,0,sizeof(mat));
    }
};

matrix mul(matrix A , matrix B) {
    matrix C;
    for(int i=0;i<=1;i++) {
        for(int j=0;j<=1;j++) {
            for(int k=0;k<=1;k++) {
                C.mat[i][j]=(C.mat[i][j]+A.mat[i][k]*B.mat[k][j])%MOD;
            }
        }
    }
    return C;
}

matrix powmul(matrix A ,int n) {
    matrix B;  //初始化为单位阵
    B.mat[0][0]=1;
    B.mat[1][1]=1;
    while(n>=1) {
        if(n&1) {
            B=mul(B,A);
        }
        A=mul(A,A);    //自身幂次
        n/=2;
    }
    return B;
}

int main()
{

    int n;
    while(~scanf("%d",&n))
    {
        if(n==-1) break;
        matrix A;
        A.mat[0][0]=A.mat[0][1]=A.mat[1][0]=1;
        A.mat[1][1]=0;
        A=powmul(A,n);
        cout<<A.mat[0][1]<<endl;
    }

    return 0;
} 
时间: 2024-07-31 22:03:02

POJ 3070 矩阵mob的相关文章

POJ 3070 矩阵快速幂

裸题,最简单fib的应用模板,算是新技能get 吧. 其实和快速幂差不多了,只是矩阵代替的递推式. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 const int maxn = 1005; 6 struct node 7 { 8 int a[2][2]; 9 void init() 10 { 11 a[0][0] = a[1][0] =

POJ 3070 矩阵快速幂解决fib问题

矩阵快速幂:http://www.cnblogs.com/atmacmer/p/5184736.html 题目链接 #include<iostream> #include<cstdio> using namespace std; typedef long long ll; #define MOD 10000 ll a[7],b[7],a0[7],b0[7]; void pow_mod(ll n) { a0[1]=a0[2]=a0[3]=1,a0[4]=0; b0[1]=b0[4]=

Fibonacci (poj 3070 矩阵快速幂)

Language: Default Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10099   Accepted: 7211 Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn ? 1 + Fn ? 2 for n ≥ 2. For example, the first ten terms of the

poj 3070 矩阵快速幂简单题

基本运用,基本是模板题. 求fi[n].       (1,1)    *( 1  ) ( 1,0)     (  0) #include<iostream> #include<cstring> using namespace std; struct juz { int bat[3][3]; int x,y; //行 列 }; juz mutp(juz a,juz b) { juz c; c.x=a.x;c.y=b.y; memset(c.bat,0,sizeof(c.bat));

poj 3070 矩阵快速乘

#include <map> #include <stdio.h> #include <iostream> using namespace std; void multiple ( int x[2][2], int y[2][2] ) { int temp[2][2] temp[0][0] = (;x[0][0]*y[0][0]+x[0][1]*y[1][0])%10000; temp[0][1] = (x[0][0]*y[0][1]+x[0][1]*y[1][1])%

poj 3070 矩阵快速幂模板

题意:求fibonacci数列第n项 1 #include "iostream" 2 #include "vector" 3 #include "cstring" 4 using namespace std; 5 6 typedef unsigned long int ULL; 7 typedef vector<ULL> vec; 8 typedef vector<vec> mat; 9 const ULL P=10000

poj 3070 Fibonacci (矩阵快速幂求斐波那契数列的第n项)

题意就是用矩阵乘法来求斐波那契数列的第n项的后四位数.如果后四位全为0,则输出0,否则 输出后四位去掉前导0,也...就...是...说...输出Fn%10000. 题目说的如此清楚..我居然还在%和/来找后四位还判断是不是全为0还输出时判断是否为0然后 去掉前导0.o(╯□╰)o 还有矩阵快速幂的幂是0时要特判. P.S:今天下午就想好今天学一下矩阵乘法方面的知识,这题是我的第一道正式接触矩阵乘法的题,欧耶! #include<cstdio> #include<iostream>

矩阵快速幂 POJ 3070 Fibonacci

题目传送门 1 /* 2 矩阵快速幂:求第n项的Fibonacci数,转置矩阵都给出,套个模板就可以了.效率很高啊 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 #include <cmath> 8 using namespace std; 9 10 const int MAXN = 1e3 + 10; 11 const int INF = 0x3f3f3f3f;

POJ 3070 Fibonacci 矩阵快速求法

就是Fibonacci的矩阵算法,不过增加一点就是因为数字很大,所以需要取10000模,计算矩阵的时候取模就可以了. 本题数据不强,不过数值本来就限制整数,故此可以0ms秒了. 下面程序十分清晰了,因为分开了几个小函数了,适合初学者参考下. #include <stdio.h> const int MOD = 10000; void mulOneMatrix(int F[2][2]) { int a = F[0][0]; int b = F[1][0]; F[0][0] = (a+b)%MOD