[bzoj4162]shlw loves matrix II
来自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的更多相关文章
- BZOJ4162:shlw loves matrix II
传送门 利用Cayley-Hamilton定理,用插值法求出特征多项式 \(P(x)\) 然后 \(M^n\equiv M^n(mod~P(x))(mod~P(x))\) 然后就多项式快速幂+取模 最 ...
- [BZOJ]4162: shlw loves matrix II
Time Limit: 30 Sec Memory Limit: 128 MB Description 给定矩阵 M,请计算 M^n,并将其中每一个元素对 1000000007 取模输出. Inpu ...
- [bzoj4161]Shlw loves matrix I
来自FallDream的博客,未经允许,请勿转载,谢谢. 给定数列 {hn}前k项,其后每一项满足 hn = a1*h(n-1) + a2*h(n-2) + ... + ak*h(n-k) 其中 a1 ...
- 【leetcode】Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- 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 ...
- leetcode-Spiral Matrix II 螺旋矩阵2之python大法好,四行就搞定,你敢信?
Spiral Matrix II 螺旋矩阵 Given an integer n, generate a square matrix filled with elements from 1 to n2 ...
- 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 ...
- 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 ...
- 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? ...
随机推荐
- git基本语法
基本用法(上) 一.实验说明 本节实验为 Git 入门第一个实验,可以帮助大家熟悉如何创建和使用 git 仓库. 二.git的初始化 在使用git进行代码管理之前,我们首先 ...
- AWS EC2服务器的HTTPS负载均衡器配置过程
AWS EC2服务器配置负载均衡器步骤: 1.普通负载均衡器 至少两台EC2实例,这里以Centos6.7系统为例 启动之后先安装个apache的httpd服务器默认80端口,或者使用其他服务 ...
- Docker_部署jenkins(dockerfile实现)
docker+jenkins开始合体! 我用的是ubuntu14.04的基础镜像,具体的这里不做赘述. 我在/tmp/目录下建了一个Dockerfile文件: touch Dockerfile vi ...
- JAVA_SE基础——62.String类的构造方法
下面我先列出初学者目前用到的构造方法 String 的构造方法: String() 创建一个空内容 的字符串对象. String(byte[] bytes) 使用一个字节数组构建一个字 ...
- JAVA_SE基础——28.封装
黑马程序员blog... 面向对象三大特征:1. 封装2. 继承3 多态. 今天我们先学习第一大特征,封装. 封装:是指隐藏对象的属性和实现细节,仅对外提供公共访问方式. 好处: 1. 将变 ...
- Tomcat(1-1)重置Tomcat8.5管理员的用户名和密码
1.访问 http://localhost:8080/,点击 [manager app],提示输入用户名和密码,admin/admin后报错. 2.解决办法:重置Tomcat8.5管理员的用户名和密 ...
- Dapper中条件为In的写法
今天用Dapper更新是用到了IN写法,园子里找了篇文章这样写到 传统sql in (1,2,3) 用dapper就这样写 conn.Query<Users>("SELECT * ...
- Opencv出现“_pFirstBlock == pHead”错误的解决方法
先说结论: opencv链接库使用错误. 1,确认VS工程属性中,opencv的链接库路径和版本正确. VS2013应该使用vc12目录,VS2012对应vc11目录.debug版和release版要 ...
- CSS简介及基本知识
(CSS)cascading style sheets:层叠样式表.级联式样式表,简称:样式表. Sheets :就是一个样式文件,它的扩展名为.css Style:外观,个性化 样式表的位置 为了学 ...
- python 杂货铺
python 杂货铺之不知道的python操作 1.交互模式下的神奇的_ windos中cmd交互模式中下(python2,python3),最近一个表达式的值赋给变量 _.这样我们就可以把它当作一个 ...