前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布

矩阵

作者头像
用户1624346
发布2018-04-17 16:22:31
5700
发布2018-04-17 16:22:31
举报
文章被收录于专栏:calmoundcalmound

POJ 3070 Fibonacci

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

题意:求矩阵的n此幂 分析:二分求

#include<stdio.h>
struct matrix
{
    int a[2][2];
    matrix()
    {
        a[0][0]=a[0][1]=a[1][0]=1;
        a[1][1]=0;
    }
};

matrix multi(matrix a,matrix b)
{
    matrix temp;
    for(int i=0;i<2;i++)
    {
        for(int j=0;j<2;j++)
        {
            temp.a[i][j]=0;
            for(int k=0;k<2;k++)
            {
                temp.a[i][j]+=a.a[i][k]*b.a[k][j];
            }
            if(temp.a[i][j]>=10000)
               temp.a[i][j]%=10000;
        }
    }
    return temp;
}
matrix power(int n)
{
    matrix temp,s;
    temp.a[0][0]=temp.a[1][1]=1;
    temp.a[1][0]=temp.a[0][1]=0;
    while(n!=0)
    {
        if(n&1)
          temp=multi(temp,s);
        n=n>>1;
        s=multi(s,s);
    }
    return temp;
}

int main()
{
    int n;
    while(scanf("%d",&n))
    {
        if(n==-1) break;
        matrix ans=power(n);
        printf("%d\n",ans.a[0][1]);
    }
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013-11-07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档