传送门

利用Cayley-Hamilton定理,用插值法求出特征多项式 \(P(x)\)

然后 \(M^n\equiv M^n(mod~P(x))(mod~P(x))\)

然后就多项式快速幂+取模

最后得到了一个关于 \(M\) 的多项式,代入 \(M^i\) 即可

# include <bits/stdc++.h>
using namespace std;
typedef long long ll; const int mod(1e9 + 7); inline int Pow(ll x, int y) {
register ll ret = 1;
for (; y; y >>= 1, x = x * x % mod)
if (y & 1) ret = ret * x % mod;
return ret;
} inline void Inc(int &x, int y) {
x = x + y >= mod ? x + y - mod : x + y;
} inline int Dec(int x, int y) {
return x - y < 0 ? x - y + mod : x - y;
} int n, m, a[55][55], b[55][55], mt[55][55], tmt[55][55], len, c[55], d[55], p[55], tmp[105], yi[55];
char str[10005]; inline int Gauss() {
register int i, j, k, inv, ans = 1;
for (i = 1; i <= n; ++i) {
for (j = i; j <= n; ++j)
if (b[j][i]) {
if (i != j) swap(b[i], b[j]), ans = mod - ans;
break;
}
for (j = i + 1; j <= n; ++j)
if (b[j][i]) {
inv = (ll)b[j][i] * Pow(b[i][i], mod - 2) % mod;
for (k = i; k <= n; ++k) Inc(b[j][k], mod - (ll)b[i][k] * inv % mod);
}
ans = (ll)ans * b[i][i] % mod;
}
return ans;
} inline void Mul(int *x, int *y, int *z) {
register int i, j, inv;
memset(tmp, 0, sizeof(tmp));
for (i = 0; i <= n; ++i)
for (j = 0; j <= n; ++j) Inc(tmp[i + j], (ll)x[i] * y[j] % mod);
for (i = m; i >= n; --i) {
inv = (ll)tmp[i] * Pow(p[n], mod - 2);
for (j = 0; j <= n; ++j) Inc(tmp[i - j], mod - (ll)p[n - j] * inv % mod);
}
for (i = 0; i <= n; ++i) z[i] = tmp[i];
} int main() {
register int i, j, k, l, inv;
scanf(" %s%d", str + 1, &n), len = strlen(str + 1), m = n << 1;
for (i = 1; i <= n; ++i)
for (j = 1; j <= n; ++j) scanf("%d", &a[i][j]);
for (i = 0; i <= n; ++i) {
memset(b, 0, sizeof(b));
for (j = 1; j <= n; ++j)
for (k = 1; k <= n; ++k)
b[j][k] = (j ^ k) ? mod - a[j][k] : Dec(i, a[j][k]);
yi[i] = Gauss();
}
for (i = 0; i <= n; ++i) {
memset(tmp, 0, sizeof(tmp)), tmp[0] = yi[i];
for (j = 0; j <= n; ++j)
if (j ^ i) {
for (k = n; k; --k) tmp[k] = Dec(tmp[k - 1], (ll)tmp[k] * j % mod);
tmp[0] = mod - (ll)tmp[0] * j % mod, inv = Pow(Dec(i, j), mod - 2);
for (k = 0; k <= n; ++k) tmp[k] = (ll)tmp[k] * inv % mod;
}
for (j = 0; j <= n; ++j) Inc(p[j], tmp[j]);
}
c[0] = d[1] = 1;
for (i = len; i; --i) {
if (str[i] == '1') Mul(c, d, c);
Mul(d, d, d);
}
memset(b, 0, sizeof(b));
for (i = 1; i <= n; ++i) mt[i][i] = 1;
for (l = 0; l <= n; ++l) {
for (i = 1; i <= n; ++i)
for (j = 1; j <= n; ++j)
Inc(b[i][j], (ll)c[l] * mt[i][j] % mod);
memset(tmt, 0, sizeof(tmt));
for (i = 1; i <= n; ++i)
for (j = 1; j <= n; ++j)
for (k = 1; k <= n; ++k)
Inc(tmt[i][k], (ll)mt[i][j] * a[j][k] % mod);
memcpy(mt, tmt, sizeof(mt));
}
for (i = 1; i <= n; ++i, putchar('\n'))
for (j = 1; j <= n; ++j) printf("%d ", b[i][j]);
return 0;
}

BZOJ4162:shlw loves matrix II的更多相关文章

  1. [bzoj4162]shlw loves matrix II

    来自FallDream的博客,未经允许,请勿转载,谢谢 给定矩阵k*k的矩阵M,请计算 M^n,并将其中每一个元素对 1000000007 取模输出. k<=50 n<=2^10000 考 ...

  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. 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 ...

  7. 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? ...

  8. LintCode 38. Search a 2D Matrix II

    Write an efficient algorithm that searches for a value in an m x n matrix, return the occurrence of ...

  9. leetcode 54. Spiral Matrix 、59. Spiral Matrix II

    54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...

随机推荐

  1. 前端IDE:VSCode + WebStorm

    VSCode 插件安装 官网:Extensions for the Visual Studio family of products: (1)拼接下载链接: https://${publisher}. ...

  2. Common xaml controls(补交作业)

    Common xaml controls 常见的xaml控件: 先上一段代码,把他们基本都实现出来: <Grid Name="MyGrid"> <Button N ...

  3. How to manage local libraries in IntelliJ IDEA

    如何在 IntelliJ IDEA 中管理本地类库 一般来说,如果项目是基于 Maven 管理工具的,我们会在 pom.xml 中添加 dependency 来管理依赖.但有时也会遇到要用的类库不在 ...

  4. User类 新增共有属性Current ID

    一.题目描述 每个用户有用户编号(id),用户名(name),密码(passwd)三个属性.其中: 用户编号(id)由系统自动顺序编号,用户名和密码都是字母.数字.符合的组合,新用户密码,默认“111 ...

  5. python全栈开发_day15_函数回调和模块

    一:函数回调 def a(fn=None): print("run1") if fn: fn() print("run 2") def b(): print(& ...

  6. F6&F7adjust the volume

    Windows Registry Editor Version 5.00[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard La ...

  7. Mac 10.12安装SVN工具SmartSVM 7.6

    说明:SVN工具没有最好的,只有用的最顺手的. 下载: (链接: https://pan.baidu.com/s/1dFGqEsT 密码: uyjx)

  8. pycharm使用github

    pycharm使用github 绑定账号 File-settings 在搜索框输入git 会出现github,然后在旁边输入你github的用户名和密码,可以点击”test”测试一下,如果出现: Co ...

  9. javac的词法分析

      1.词法分析将Java源文件的字符流转变为对应的Token流.   JavacParser类规定了哪些词是符合Java语言规范规定的词,而具体读取和归类不同词法的操作由Scanner类来完成.   ...

  10. python-组播

    #!/usr/bin/python #coding=utf-8 #发送端 import sys,struct,socket from time import sleep message="h ...