HDU 4686 Arc of Dream(矩阵)

Arc of Dream

【题目链接】Arc of Dream

【题目类型】矩阵

&题解:

这题你做的复杂与否很大取决于你建的矩阵是什么样的,膜一发kuangbin大神的矩阵:

还有几个坑点:当n是0 输出0;建矩阵时是相乘的一定要取模M,因为如果不取模最大的情况是1e9*2e9*2e9,爆long long 这块坑了我好长时间.

&代码:

#include <cstdio>
#include <bitset>
#include <iostream>
#include <set>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
typedef long long ll;
const int M= 1e9 +7;
ll n,a0,ax,ay,b0,bx,by;
struct mat
{
    ll m[8][8];
}A;
ll tb[8];
void Init()
{
    tb[0]=a0,tb[1]=b0,tb[2]=a0*b0%M,tb[3]=a0*b0%M,tb[4]=1;
    memset(A.m,0,sizeof(A.m));
    A.m[0][0]=ax,A.m[4][0]=ay;
    A.m[1][1]=bx,A.m[4][1]=by;
    A.m[0][2]=ax*by%M,A.m[1][2]=ay*bx%M,A.m[2][2]=ax*bx%M,A.m[4][2]=ay*by%M;
    for(int i=0;i<5;i++) A.m[i][3]=A.m[i][2];
    A.m[3][3]=A.m[4][4]=1;
}
//debug
void DF()
{
    for(int i=0;i<5;i++){
        for(int j=0;j<5;j++){
            printf("%4lld ",A.m[i][j]);
        }
        cout<<endl;
    }
}
mat Mul(mat a,mat b)
{
    mat c;
    for(int i=0;i<5;i++)
    for(int j=0;j<5;j++){
        c.m[i][j]=0;
        for(int k=0;k<5;k++){
            c.m[i][j]=(c.m[i][j]+a.m[i][k]*b.m[k][j])%M;
        }
    }
    return c;
}
mat bPow(mat a,ll z)
{
    mat un;
    for(int i=0;i<5;i++)
        for(int j=0;j<5;j++)
            un.m[i][j]=(i==j);
    while(z){
        if(z&1)
            un=Mul(un,a);
        a=Mul(a,a);
        z>>=1;
    }
    return un;
}
int main()
{
//    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    //("E:1.txt","r",stdin);
    while(cin>>n){
        cin>>a0>>ax>>ay;
        cin>>b0>>bx>>by;
        Init();
        if(n>0){
            A=bPow(A,n-1);
        }
        else{
            cout<<0<<endl;
            continue;
        }
//        DF();
        ll ans=0;
        for(int i=0;i<5;i++){
            ans=(ans+A.m[i][3]*tb[i])%M;
        }
        cout<<ans%M<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/s1124yy/p/6665838.html