Bzoj2326 [hnoi2011]数学作业

矩阵乘法加速

Posted by yjjr's blog on March 11, 2018

标签:矩阵乘法

题目

题目传送门

这里写图片描述

分析

设f[n]表示Concatenate(1,n)

那么

可以用矩阵加速

这里写图片描述

code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#define mem(x,num) memset(x,num,sizeof x)
#define reg(x) for(int i=last[x];i;i=e[i].next)
#define lson (x<<1)
#define rson (x<<1|1)
#define mid ((l+r)>>1)
#define pi acos(-1)
#define mp make_pair
#define pb push_back
#define pa pair<int,int>
#define fi first
#define se second
#define lowbit(x) ((x)&(-x))
using namespace std;
inline ll read(){
	ll f=1,x=0;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
inline ll getgcd(ll x,ll y){return !x?y:getgcd(y%x,x);}
inline ll getlcm(ll x,ll y){return x/getgcd(x,y)*y;}
inline ll mul(ll x,ll y,ll p){
	ll re=0;
	while(y){
	   	if(y&1)re=(re+x)%p;
		y>>=1,x=(x<<1)%p;
	}
	return re;
}
//**********head by yjjr**********
ll n,mod,a[4][4],b[4][4],t=10;
void Mul(ll a[4][4],ll b[4][4],ll s[4][4]){
	ll tmp[4][4];
	rep(i,1,3)
		rep(j,1,3){
			tmp[i][j]=0;
			rep(k,1,3)tmp[i][j]=(tmp[i][j]+mul(a[i][k],b[k][j],mod))%mod;
		}
	rep(i,1,3)
		rep(j,1,3)s[i][j]=tmp[i][j];
}
void cal(ll t,ll last){
	mem(b,0);
	b[1][1]=t;b[2][1]=b[2][2]=b[3][1]=b[3][2]=b[3][3]=1;
	ll y=last-t/10+1;
	while(y){
		if(y&1)Mul(a,b,a);
		Mul(b,b,b),y>>=1;
	}
}
int main()
{
	n=read(),mod=read();
	rep(i,1,3)a[i][i]=1;
	while(n>=t){
		cal(t,t-1);
		t*=10;
	}
	cal(t,n);
	cout<<a[3][1]<<endl;
	return 0;
}
本文可以转载,但必须附上原文链接,否则你会终生找不到妹子!!!欢迎关注我的CSDN: ahyjjr