BZOJ4162:shlw loves matrix II
传送门
利用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的更多相关文章
- [bzoj4162]shlw loves matrix II
来自FallDream的博客,未经允许,请勿转载,谢谢 给定矩阵k*k的矩阵M,请计算 M^n,并将其中每一个元素对 1000000007 取模输出. k<=50 n<=2^10000 考 ...
- [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 ...
- 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 STL
pog loves szh II Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php? ...
- 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 ...
- leetcode 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
随机推荐
- Jmeter使用吞吐量控制器实现不同的用户操纵不同的业务
一.需求 需求:博客系统,模拟用户真实行为,80%的用户阅读文章,20%的用户创建文章,创建文章的用户随机的删除或者修改文章. 二.脚本实现 80%的用户查看文章 20%用户创建文章 根据post_i ...
- iOS 之新特性界面
1.什么事新特性界面? 新特性界面就是第一次下载程序出现的界面,他的用途是帮助用户快速了解这款APP,所有说还是很有必要学一下的. 2.如何实现新特性界面? 实现思路:从本质上看,新特性界面就是一个全 ...
- tp5.0 根据经纬度 获取附近信息
自己备注一下 /* *参数说明: *$lng 经度 *$lat 纬度 *$distance 周边半径 默认是500米(0.5Km) */ public function returnSquarePoi ...
- 论文分享NO.2(by_xiaojian)
论文分享第二期-2019.03.26 NIPS2015,Spatial Transformer Networks,STN,空间变换网络
- (转)Python 运算符
原文:https://blog.csdn.net/liang19890820/article/details/69690954 简述 在 Python 中,运算符是执行算术或逻辑运算的特殊符号,操作的 ...
- (转)oracle linux 7 安装oracle 12c
原文:https://blog.csdn.net/jiuyun1986/article/details/53589446 https://blog.csdn.net/admin_root1/artic ...
- Python 两种获取文件大小的方法
import os r=os.path.getsize("/root/catbird1.stl") f=open("/root/catbird1.stl",&q ...
- str_split 分隔中文出现乱码 替代函数
function mbstringtoarray($str,$charset) { $strlen=mb_strlen($str); while($strlen){ $array[]=mb_subst ...
- preg_match 与 preg_match_all
案例一: <?php $str = 'abcdef123456'; preg_match('/[a-z1-9]+/', $str, $res); var_dump($res); preg_mat ...
- MapReduce原理——分而治之
一.MapReduce简介 二.MapReduce并行处理的基本过程 三.MapReduce实际处理流程 四.一个job的运行流程 一.MapReduce简介 MapReduce是一种并行可扩展计算模 ...