练习题:垒骰子(矩阵快速幂)

参考文章:https://blog.csdn.net/qq_34594236/article/details/53616283

举一反三:好好思考互斥的关系矩阵表现形式
计蒜客的上一道题:https://www.jisuanke.com/course/737/37733 1-m m n 1-n形式也很值得参考

以下是AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int a[]={0,4,5,6,1,2,3}; //对面
const int mod=1000000007;

typedef struct
{
    long long d[7][7];
}node;

node mul(node p1,node p2)
{
    node ans;
    for(int i=1;i<=6;++i)
    for(int j=1;j<=6;++j)
    {
        long long res=0;

        for(int k=1;k<=6;++k)
        res+=p1.d[i][k]*p2.d[k][j];

        ans.d[i][j]=res%mod;
    }
    return ans;
}

node pow(node a,int n)
{
    node ans;
    for(int i=1;i<=6;++i)
    for(int j=1;j<=6;++j)
    {
    if(i==j)
    ans.d[i][j]=1;
    else ans.d[i][j]=0;
    }

    while(n)
    {
        if(n&1)
        ans=mul(ans,a);
        a=mul(a,a);
        n/=2;
    }
    return ans;
}

int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    int x,y;
    node G;
    for(int i=1;i<=6;++i)
    for(int j=1;j<=6;++j)
    G.d[i][j]=4;

    for(int i=0;i<m;++i)
    {
        scanf("%d%d",&x,&y);
        G.d[a[x]][y]=0; G.d[a[y]][x]=0;
    }


    node res=pow(G,n-1);
    long long ans=0;
    for(int i=1;i<=6;++i)
    for(int j=1;j<=6;++j)
    {
        ans+=res.d[j][i]*4;
    }
    printf("%lld\n",ans%mod);
    return 0;
} 
另贴矩阵构造:https://blog.csdn.net/qq_42211531/article/details/81098262
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值