P1397 [NOI2013] matrix game

Portal

Obviously we can take the first moments of power and then quickly fly $ T $

Look at the problem solution found because of the special nature of the problem matrix can therefore Euler descending on the number of matrices

However, I do not understand prove, so I chose to practice violence fucks

Decimal power quickly, and then note the constant, as well as the order of the matrix multiplication, not backwards

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
inline int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
    while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); }
    return x*f;
}
const int N=1e6+7,mo=1e9+7;
char n[N],m[N];
struct Matrix {
    int a[3][3];
    Matrix () { memset(a,0,sizeof(a)); }
    inline Matrix operator * (const Matrix &tmp) const {
        Matrix res;
        res.a[1][1]=(1ll*a[1][1]*tmp.a[1][1]+1ll*a[1][2]*tmp.a[2][1])%mo;
        res.a[1][2]=(1ll*a[1][1]*tmp.a[1][2]+1ll*a[1][2]*tmp.a[2][2])%mo;
        res.a[2 ] [ . 1 ] = (1LL * A [ 2 ] [ . 1 ] * tmp.a [ . 1 ] [ . 1 ] + 1LL * A [ 2 ] [ 2 ] tmp.a * [ 2 ] [ . 1 ])% Mo; 
        res.a [ 2 ] [ 2 ] = (1LL * A [ 2 ] [ . 1 ] * tmp.a [ . 1 ] [ 2 ] * 1LL + A [ 2 ] [ 2 ] * tmp.a [ 2 ] [ 2 ] )% Mo;
         // optimization of loop unrolling constant 
        return RES; 
    } 
} F., G, Ans; 
the Matrix KSM (the Matrix X,char *y)//十进制快速幂
{
    Matrix res,t; res.a[1][1]=res.a[2][2]=1;
    for(int i=strlen(y+1);i;i--)
    {
        t=x;
        if(y[i]=='9') { t=t*t; t=t*t; t=t*t; t=t*x; res=res*t; }
        else if(y[i]=='8') { t=t*t; t=t*t; t=t*t; res=res*t; }
        else for(int j=1;j<=y[i]-'0';j++) res=res*x;
        t=x; x=x*x; x=x*x; x=x*x; x=x*t; x=x*t;
    }
    return res;
}
void Minus(char *s)//把数减一
{
    for(int i=strlen(s+1);i;i--)
        if(s[i]=='0') s[i]='9';
        else { s[i]=s[i]-1; break; }
}
int main()
{
    int a,b,c,d; scanf("%s",n+1); scanf("%s",m+1);
    a=read(),b=read(),c=read(),d=read();
    Ans.a[1][1]=Ans.a[1][2]=1;
    F.a[1][1]=a; F.a[2][1]=b; F.a[2][2]=1;
    G.a[1][1]=c; G.a[2][1]=d; G.a[2][2]=1;
    Minus(n); Minus(m);
    F=ksm(F,m); G=F*G; G=ksm(G,n);
    Ans=Ans*G*F;
    printf("%d\n",Ans.a[1][1]);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/LLTYYC/p/11521638.html