来自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. DML数据操作语言之查询(二)

    当我们查询出了N条记录之后 ,我们知道一共是几条记录,或者这些记录某一字段(列值)的最大值,最小值,平均值等,就可以使用聚合函数. 1.聚合函数 聚合函数会将null 排除在外.但是count(*)例 ...

  2. Django rest framework源码分析(4)----版本

    版本 新建一个工程Myproject和一个app名为api (1)api/models.py from django.db import models class UserInfo(models.Mo ...

  3. 【深度学习】深入理解Batch Normalization批标准化

    这几天面试经常被问到BN层的原理,虽然回答上来了,但还是感觉答得不是很好,今天仔细研究了一下Batch Normalization的原理,以下为参考网上几篇文章总结得出. Batch Normaliz ...

  4. 其他—cooki和session

    cookie Cookie的由来 大家都知道HTTP协议是无状态的. 无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应情况直接影响,也不 ...

  5. angular2 学习笔记 の 移动端开发 ( 手势 )

    更新 : 2018-01-31 (hammer 的坑) hammer 的 pinch 在某种情况下会自动触发 panEnd,很奇葩. 解决方法就是记入时间呗 refer : https://githu ...

  6. SLF4J - 借助SLF4J, 统一适配所有日志实现为logback日志实现的实践

    一.屏蔽各种日志实现,去掉各种日志实现的实现依赖 二.引入slf4j和各种日志实现的适配器 1.引入slf4j 2.引入各种日志实现的适配器(适配到slf4j) 3.引入logback 引入logba ...

  7. multiprocessing.Process() ----------python中的多进程

    python 当中 使用封装好的 multiprocessing 为我们实现创建多进程任务. 1 Process()方法创建子进程 使用multiprocessing.Process() 方法产生一个 ...

  8. 老男孩python学习之作业一购物小程序

    想学编程由来已久 始终没有个结果,痛心不已 如今再次捡起来,望不负期望,不负岁月 ......一万字的废话...... 先介绍一下我的自学课程吧 "路飞学城"的<python ...

  9. Java基础语法<十> Jar文件

    1 JAR文件            java归档文件,压缩的            jdk/bin jar工具制作jar文件              jar程序选项 1.1清单文件         ...

  10. hdu1010 Tempter of the Bone---DFS+奇偶剪枝

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目描述:根据地图,'S'为开始位置,'D'为门的位置,' . '为空地,'X'为墙,不能经过 ...