码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 1005 矩阵快速幂

时间:2016-04-22 18:16:52      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define mod 1000000007
#define inf 999999999
//#pragma comment(linker, "/STACK:102400000,102400000")
int scan()
{
    int res = 0 , ch ;
    while( !( ( ch = getchar() ) >= 0 && ch <= 9 ) )
    {
        if( ch == EOF ) return 1 << 30 ;
    }
    res = ch - 0 ;
    while( ( ch = getchar() ) >= 0 && ch <= 9 )
        res = res * 10 + ( ch - 0 ) ;
    return res ;
}
struct is
{
    ll a[10][10];
};
is juzhenmul(is a,is b,ll hang ,ll lie)
{
    int i,t,j;
    is ans;
    memset(ans.a,0,sizeof(ans.a));
    for(i=1;i<=hang;i++)
    for(t=1;t<=lie;t++)
    for(j=1;j<=lie;j++)
    {
        ans.a[i][t]+=(a.a[i][j]*b.a[j][t])%7;
    }
    return ans;
}
is quickpow(is ans,is a,ll x)
{
    while(x)
    {
        if(x&1)  ans=juzhenmul(ans,a,2,2);
        a=juzhenmul(a,a,2,2);
        x>>=1;
    }
    return ans;
}
int main()
{
    is ans,base;
    ll x,y,z,i,t;
    while(scanf("%lld%lld%lld",&x,&y,&z)!=EOF)
    {
        if(x==0&&y==0&&z==0)break;
        if(z<3)
        {printf("1\n");
        continue;}
        memset(ans.a,0,sizeof(ans.a));
        ans.a[1][1]=1;
        ans.a[2][2]=1;
        //printf("%lld %lld\n%lld %lld\n",ans.a[1][1],ans.a[1][2],ans.a[2][1],ans.a[2][2]);
        base.a[1][1]=x;
        base.a[1][2]=1;
        base.a[2][1]=y;
        base.a[2][2]=0;
        ans=quickpow(ans,base,z-2);
        printf("%lld\n",(ans.a[1][1]+ans.a[2][1])%7);
    }
    return 0;
}
View Code

 

hdu 1005 矩阵快速幂

标签:

原文地址:http://www.cnblogs.com/jhz033/p/5422291.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!