设为首页 加入收藏

TOP

HDU4686[矩阵快速幂]
2014-11-23 20:25:34 来源: 作者: 【 】 浏览:9
Tags:HDU4686 矩阵 快速

只能说自己以前做的那个矩阵快速幂太弱了,或者推的还不够。

这道题把f(i),s(i),a(i),b(i)都合在一块了,一个5*5的矩阵。

另外,反正在hdu就用%I64d,别管别的了。 keng...

我的paw,multiply模板肯定没错,连续的WA是坑在__int64上。

练习的题目太少,推敲涉及比较多的变量,赞一个

 
#include    
#include    
#include    
using namespace std;  
  
typedef __int64 LL;  
const LL maxn=1e18+7;  
const LL mod=1000000007;  
  
typedef struct M  
{  
    LL x[6][6];  
}mat;  
  
struct M multiply(struct M a,struct M b)  
{  
    struct M ret;  
    for(int i = 0;i<5;i++)  
    {  
        for(int j = 0;j <5;j++)  
        {  
            LL tem = 0;  
            for(int k = 0;k < 5;k++)  
                tem = (tem+a.x[i][k]*b.x[k][j])%mod;  
            ret.x[i][j] = tem%mod;//这地方取模注意一下.   
        }  
    }  
    return ret;  
}  
  
struct M paw(struct M a,long long t)  
{  
    if(t==1)  
        return a;  
    else  
    {  
        struct M b=paw(a,t/2);  
        if(t&1)  
        {  
            return multiply(multiply(b,b),a);  
        }  
        else  
            return multiply(b,b);  
    }  
}  
  
mat org,add;  
  
int main()  
{  
    LL n,a0,ax,ay;  
    LL b0,bx,by;  
    while(scanf("%I64d",&n)!=EOF)  
    {  
        scanf("%I64d%I64d%I64d",&a0,&ax,&ay);  
        scanf("%I64d%I64d%I64d",&b0,&bx,&by);  
        if(n==0) {printf("0\n");continue;}  
        memset(org.x,0,sizeof(org.x));  
        memset(add.x,0,sizeof(add.x));  
  
        add.x[0][0]=1; add.x[0][1]=0;         add.x[0][2]=0;      add.x[0][3]=0;      add.x[0][4]=0;  
        add.x[1][0]=1; add.x[1][1]=ax*bx%mod; add.x[1][2]=0;      add.x[1][3]=0;      add.x[1][4]=0;  
        add.x[2][0]=0; add.x[2][1]=ax*by%mod; add.x[2][2]=ax%mod; add.x[2][3]=0;      add.x[2][4]=0;  
        add.x[3][0]=0; add.x[3][1]=ay*bx%mod; add.x[3][2]=0;      add.x[3][3]=bx%mod; add.x[3][4]=0;  
        add.x[4][0]=0; add.x[4][1]=ay*by%mod; add.x[4][2]=ay%mod; add.x[4][3]=by%mod; add.x[4][4]=1;  
  
        org.x[0][0]=a0*b0%mod; org.x[0][1]=(((a0*ax+ay)%mod)*((b0*bx+by)%mod))%mod;  
        org.x[0][2]=(a0*ax+ay)%mod; org.x[0][3]=(b0*bx+by)%mod; org.x[0][4]=1;  
  
        mat ans;  
        if(n==1) ans=org;  
        else  
        {  
            ans=multiply(org,paw(add,n-1));  
        }  
        printf("%I64d\n",ans.x[0][0]%mod);  
     }  
    return 0;  
}  

#include 
#include 
#include 
using namespace std;

typedef __int64 LL;
const LL maxn=1e18+7;
const LL mod=1000000007;

typedef struct M
{
    LL x[6][6];
}mat;

struct M multiply(struct M a,struct M b)
{
    struct M ret;
    for(int i = 0;i<5;i++)
    {
        for(int j = 0;j <5;j++)
        {
            LL tem = 0;
            for(int k = 0;k < 5;k++)
                tem = (tem+a.x[i][k]*b.x[k][j])%mod;
            ret.x[i][j] = tem%mod;//这地方取模注意一下.
        }
    }
    return ret;
}

struct M paw(struct M a,long long t)
{
    if(t==1)
        return a;
    else
    {
        struct M b=paw(a,t/2);
        if(t&1)
        {
            return multiply(multiply(b,b),a);
        }
        else
            return multiply(b,b);
    }
}

mat org,add;

int main()
{
    LL n,a0,ax,ay;
    LL b0,bx,by;
    while(scanf("%I64d",&n)!=EOF)
    {
        scanf("%I64d%I64d%I64d",&a0,&ax,&ay);
        scanf("%I64d%I64d%I64d",&b0,&bx,&by);
        if(n==0) {printf("0\n");continue;}
        memset(org.x,0,sizeof(org.x));
        memset(add.x,0,sizeof(add.x));

        add.x[0][0]=1; add.x[0][1]=0;         add.x[0][2]=0;      add.x[0][3]=0;      add.x[0][4]=0;
        add.x[1][0]=1; add.x[1][1]=ax*bx%mod; add.x[1][2]=0;      add.x[1][3]=0;      add.x[1][4]=0;
        add.x[2][0]=0; add.x[2][1]=ax*by%mod; add.x[2][2]=ax%mod; add.x[2][3]=0;      add.x[2][4]=0;
        add.x[3][0]=0; add.x[3][1]=ay*bx%mod; add.x[3][2]=0;      add.x[3][3]=bx%mod; add.x[3][4]=0;
        add.x[4][0]=0; add.x[4][1]=ay*by%mod; add.x[4][2]=ay%mod; add.x[4][3]=by%mod; add.x[4][4]=1;

        org.x[0][0]=a0*b0%mod; org.x[0][1]=(((a0*ax+ay)%mod)*((b0*bx+by)%mod))%mod;
        org.x[0][2]=(a0*ax+ay)%mod; org.x[0][3]=(b0*bx+by)%mod; org.x[0][4]=1;

        mat ans;
        if(n==1) ans=org;
        else
        {
            ans=multiply(org,paw(add,n-1));
        }
        printf("%I64d\n",ans.x[0][0]%mod);
     }
    return 0;
}


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇BUREK 下一篇hdu 2187

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·我的Linux内核学习笔 (2025-12-26 22:21:10)
·如何评价腾讯开源的 (2025-12-26 22:21:07)
·为什么TCP网络编程中 (2025-12-26 22:21:04)
·Python 数据分析与可 (2025-12-26 21:51:20)
·从零开始学Python之 (2025-12-26 21:51:17)