来自FallDream的博客,未经允许,请勿转载,谢谢


给定矩阵k*k的矩阵M,请计算 M^n,并将其中每一个元素对 1000000007 取模输出。 k<=50 n<=2^10000

考虑求出矩阵的特征多项式,这点我们可以通过带入$\lambda=x0..xk$,求出矩阵的行列式,然后通过插值求出多项式。

然后搬出一个很厉害的定理:f(A)=0 其中f(x)是特征多项式,A是矩阵,所以我们可以把所求的$x^{n}$对f(x)取膜,从而让答案变成一个k-1次多项式。然后我们把原矩阵带进去就行了。

插值的复杂度是$O(n^{4})$,然后后面那部分的复杂度是$k^{2}logn$

然后做了这道题好像懂了怎么在O(klogklogn)内做完"常系数线性递推",貌似还要nlogn的多项式取余 真麻烦TAT

#include<iostream>
#include<cstdio>
#include<cstring>
#define ll long long
#define mod 1000000007
using namespace std;
inline int read()
{
int x = ; char ch = getchar();
while(ch < '' || ch > '')ch = getchar();
while(ch >= '' && ch <= ''){x = x * + ch - '';ch = getchar();}
return x;
} char st[];
int y[],k; int pow(int x,int k)
{
int sum=;
for(;k;k>>=,x=1LL*x*x%mod)
if(k&) sum=1LL*sum*x%mod;
return sum;
}
struct Matrix
{
int s[][];
Matrix operator*(Matrix b)
{
Matrix c;memset(c.s,,sizeof(c.s));
for(int i=;i<=k;i++)
for(int l=;l<=k;l++) if(s[i][l])
for(int j=;j<=k;j++)
c.s[i][j]=(c.s[i][j]+1LL*s[i][l]*b.s[l][j])%mod;
return c;
}
int calc()
{
int sum=,j;
for(int i=;i<=k;i++)
{
for(j=i;j<=k;j++)
if(s[j][i])
{
if(j!=i)
{
sum=mod-sum;
for(int l=;l<=k;l++)
swap(s[j][l],s[i][l]);
}
break;
}
if(j==k+) return ;
for(int j=i+;j<=k;j++)
{
int inv=1LL*pow(s[i][i],mod-)*s[j][i]%mod;
for(int l=i;l<=k;l++)
s[j][l]=(1LL*s[i][l]*inv%mod-s[j][l]+mod)%mod;
}
}
for(int i=;i<=k;i++) sum=1LL*sum*s[i][i]%mod;
return sum;
}
}a,b,ans; struct poly
{
int s[];
poly(int x=){memset(s,,sizeof(s));s[]=x;}
poly operator^(int x)
{
poly c;
for(int i=k<<;i;i--)
c.s[i]=(s[i-]+1LL*x*s[i])%mod;
c.s[]=(1LL*s[]*x)%mod;
return c;
}
poly operator*(int x)
{
poly c();
for(int i=;i<=k<<;i++)
c.s[i]=1LL*s[i]*x%mod;
return c;
}
poly operator+(poly b)
{
poly c();
for(int i=;i<=k<<;i++)
c.s[i]=(s[i]+b.s[i])%mod;
return c;
}
poly operator*(poly b)
{
poly c();
for(int i=;i<=k;i++)
for(int j=;j<=k;j++)
c.s[i+j]=(c.s[i+j]+1LL*s[i]*b.s[j])%mod;
return c;
}
friend poly operator%(poly a,poly b)
{
for(int i=k;i>=;i--)
{
int t=1LL*a.s[i+k]*pow(b.s[k],mod-)%mod;
for(int j=;j<=k;j++)
a.s[i+j]=(a.s[i+j]-1LL*b.s[j]*t%mod+mod)%mod;
}
return a;
}
}F(),Ans(); int main()
{
scanf("%s",st+);k=read();
for(register int i=;i<=k;i++)
for(register int j=;j<=k;j++)
b.s[i][j]=a.s[i][j]=read();
for(register int t=;t<=k;t++)
{
memcpy(b.s,a.s,sizeof(b.s));
for(int i=;i<=k;i++)
b.s[i][i]=(b.s[i][i]-t+mod)%mod;
y[t]=b.calc();
}
for(register int t=;t<=k;t++)
{
poly tmp();
for(register int i=;i<=k;i++) if(i!=t)
tmp=tmp^(mod-i),tmp=tmp*pow((t-i+mod)%mod,mod-);
tmp=tmp*y[t];F=F+tmp;
}
poly x();x.s[]=;
for(int i=strlen(st+);i;i--)
{
if(st[i]=='') Ans=Ans*x%F;
x=x*x%F;
}
memset(b.s,,sizeof(b.s));
for(int i=;i<=k;i++) b.s[i][i]=;
for(int t=;t<k;t++,b=a*b)
for(int i=;i<=k;i++)
for(int j=;j<=k;j++)
ans.s[i][j]=(ans.s[i][j]+1LL*Ans.s[t]*b.s[i][j])%mod;
for(register int i=;i<=k;i++)
for(register int j=;j<=k;j++)
printf("%d%c",ans.s[i][j],j!=k?' ':'\n');
return ;

[bzoj4162]shlw loves matrix II的更多相关文章

  1. BZOJ4162:shlw loves matrix II

    传送门 利用Cayley-Hamilton定理,用插值法求出特征多项式 \(P(x)\) 然后 \(M^n\equiv M^n(mod~P(x))(mod~P(x))\) 然后就多项式快速幂+取模 最 ...

  2. [BZOJ]4162: shlw loves matrix II

    Time Limit: 30 Sec  Memory Limit: 128 MB Description 给定矩阵 M,请计算 M^n,并将其中每一个元素对 1000000007 取模输出. Inpu ...

  3. [bzoj4161]Shlw loves matrix I

    来自FallDream的博客,未经允许,请勿转载,谢谢. 给定数列 {hn}前k项,其后每一项满足 hn = a1*h(n-1) + a2*h(n-2) + ... + ak*h(n-k) 其中 a1 ...

  4. 【leetcode】Spiral Matrix II

    Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...

  5. 59. Spiral Matrix && Spiral Matrix II

    Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...

  6. leetcode-Spiral Matrix II 螺旋矩阵2之python大法好,四行就搞定,你敢信?

    Spiral Matrix II 螺旋矩阵 Given an integer n, generate a square matrix filled with elements from 1 to n2 ...

  7. Search a 2D Matrix | & II

    Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix, ret ...

  8. hdu 5265 pog loves szh II

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5265 pog loves szh II Description Pog and Szh are pla ...

  9. hdu 5265 pog loves szh II STL

    pog loves szh II Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php? ...

随机推荐

  1. Python多线程案例

    from time import ctime,sleep import threading def music(): for i in range(2): print ("I was lis ...

  2. Android网络传输中必用的两个加密算法:MD5 和 RSA 及Base64加密总结

    (1)commons-codec包简介 包含一些通用的编码解码算法.包括一些语音编码器,Hex,Base64.MD5 一.md5.base64.commons-codec包 commons-codec ...

  3. L2 约束的最小二乘学习法

    \[ \begin{align*} &J_{LS}{(\theta)} = \frac { 1 }{ 2 } { \left\| \Phi \theta - y \right\| }^{ 2 ...

  4. shell中冒号 : 用途说明

    我们知道,在Linux系统中,冒号(:)常用来做路径的分隔符(PATH),数据字段的分隔符(/etc/passwd)等.其实,冒号(:)在Bash中也是一个内建命令,它啥也不做,是个空命令.只起到占一 ...

  5. 通过URL传递PDF名称参数显示PDF

    1 <%@ page language="java" import="java.util.*,java.io.*" 2 pageEncoding=&quo ...

  6. EasyUI中easyui-combobox的onchange事件。

    html: <select id="cbox" class="easyui-combobox" name="dept" style=& ...

  7. webpack你值得拥有-从四个核心配置谈起

    很久没有发文章了,但是强调一点,大-熊同学最近可没闲着.学习算法,复习计算机网络,也顺便学习了一下webpack,看了看操作系统(没办法,都没学,要是不学连实习笔试都过不了,伤心--).本来比较纠结是 ...

  8. Mego开发文档 - 建模高级主题

    建模高级主题 在建模过程中我们还有许多其他情况,这里列出本框架中的有用特性来用于解决此类问题. 函数映射 我们可以将指定的CLR函数映射到数据库中的系统函数或自定义函数,该特性用于补充框架中未提供的数 ...

  9. nodejs(1-1)

    http://www.runoob.com/nodejs/nodejs-tutorial.html

  10. shiro中CacheManager相关的类结构介绍,提供redis Cache实现

    cacheManager主要用于对shiro中的session.realm中的认证信息.授权信息进行缓存. 1.类结构 2.接口及类介绍 CacheManager 提供根据名字获取cache的作用. ...