zoukankan      html  css  js  c++  java
  • generator1

    传送门:

    矩阵快速幂+10进制处理。

    本题不能用欧拉降幂,除非矩阵对角化!

    #include <bits/stdc++.h>
    using namespace std;
    #define re register
    #define ll long long
    void read(int &a)
    {
        a=0;
        int d=1;
        char ch;
        while(ch=getchar(),ch>'9'||ch<'0')
            if(ch=='-')
                d=-1;
        a=ch-'0';
        while(ch=getchar(),ch>='0'&&ch<='9')
            a=a*10+ch-'0';
        a*=d;
    }
    void write(int x)
    {
        if(x<0)
            putchar(45),x=-x;
        if(x>9)
            write(x/10);
        putchar(x%10+'0');
    }
    struct note
    {
        ll a[5][5];
    };
    note ans,a;
    ll mod;
    void init(int x,int y)
    {
        a.a[1][1]=x;
        a.a[1][2]=y;
        a.a[2][1]=1;
        a.a[2][2]=0;
    }
    note Mat(note x,note y)
    {
        note c;
        for(re int i=1;i<=2;i++)
            for(re int j=1;j<=2;j++)
                c.a[i][j]=0;
        for(re int i=1;i<=2;i++)
            for(re int j=1;j<=2;j++)
                for(re int k=1;k<=2;k++)
                    c.a[i][j]=(c.a[i][j]+x.a[i][k]*y.a[k][j])%mod;
        return c;
    }
    note quick(int k)
    {
        note res;
        for(re int i=1;i<=2;i++)
            for(re int j=1;j<=2;j++)
               res.a[i][j]=0;
        for(re int i=1;i<=2;i++)
            res.a[i][i]=1;
        note base=a;
        while(k)
        {
            if(k&1)
                res=Mat(res,base);
            base=Mat(base,base);
            k>>=1;
        }
        return res;
    }
    int main()
    {
        int x0,x1,x,y;
        string s;
        read(x0),read(x1),read(x),read(y);
        cin>>s;
        scanf("%lld",&mod);
        init(x,y);
        for(re int i=1;i<=2;i++)
            for(re int j=1;j<=2;j++)
               ans.a[i][j]=0;
        for(re int i=1;i<=2;i++)
            ans.a[i][i]=1;
        for(re int i=s.size()-1;i>=0;i--)
        {
            ans=Mat(ans,quick(s[i]-'0'));
            a=quick(10);
        }
        //a=quick(0);
        //ans=Mat(ans,a);
        //a=quick(10);
        //ans=Mat(ans,a);
        /*for(re int i=1;i<=2;i++)
        {
            for(re int j=1;j<=2;j++)
                cout<<a.a[i][j]<<" ";
            cout<<endl;
        }*/
        printf("%lld",(ans.a[2][1]*x1%mod+ans.a[2][2]*x0%mod)%mod);
        return 0;
    }
  • 相关阅读:
    追寻缺失的大学精神 一个民族需要关注天空的人
    图论简介
    18个分形图形的GIF动画演示
    平行宇宙
    eclipse经常出现——未响应!!!
    单例模式
    Java内存区域
    编译与解释(java)
    正则表达式判断QQ号格式是否正确
    正则表达式判断手机号格式是否正确
  • 原文地址:https://www.cnblogs.com/acm1ruoji/p/11289530.html
Copyright © 2011-2022 走看看