【HNOI2011】【BZOJ2326】数学作业

Description
【HNOI2011】【BZOJ2326】数学作业_第1张图片

Input

Output

Sample Input

Sample Output

HINT

Source

矩阵乘法

怎么觉得以前做过..是忘了放题解了吗..
Source里就是题解T_T

#include
#include
#include
#include
#include
#define MAXN 10
#define LL long long
using namespace std;
const int size=3;
LL n,m;
LL Mult(LL a,LL b)
{
    LL ret=0;a%=m;
    for (LL i=b;i;i>>=1,a=(a<<1)%m) if (i&1)    ret+=a,ret%=m;
    return ret;
}
struct matrix
{
    LL a[MAXN][MAXN];
    matrix()
    {
        memset(a,0,sizeof(a));
    }
    friend matrix operator *(matrix A,matrix B)
    {
        matrix ret;
        for (int i=1;i<=size;i++)
            for (int j=1;j<=size;j++)
                for (int k=1;k<=size;k++)
                    ret.a[i][j]=(ret.a[i][j]+Mult(A.a[i][k],B.a[k][j]))%m;
        return ret;
    }
}a,b;
void calc(LL x,LL last)
{
     memset(b.a,0,sizeof(b.a));b.a[1][1]=x;b.a[2][1]=b.a[2][2]=b.a[3][1]=b.a[3][2]=b.a[3][3]=1;
     LL y=last-x/10+1;
     for (LL i=y;i;i>>=1,b=b*b) if (i&1)    a=a*b;

}
int main()
{
    cin>>n>>m;LL cnt=10;
    for (int i=1;i<=3;i++)  a.a[i][i]=1;
    while (cnt<=n)  calc(cnt,cnt-1),cnt*=10;
    calc(cnt,n);cout<3][1]<

你可能感兴趣的:(随便搞搞)