bzoj3583: 杰杰的女性朋友 && 4362: Graph
Description
Input
Output
Sample Input
2 5 4 3
7 9 2 4
0 1 5 2
6 3 9 2
2147483647 1000000001 233522 788488
10
1 1 0
2 2 1
2 4 5
4 3 10
3 4 50
1 5 1000
Sample Output
51
170107227
271772358
34562176
890241289
HINT
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cassert>
#include<cstring>
#include<algorithm>
#define maxn 21
#define mod 1000000007
using namespace std;
char ch;
bool ok;
void read(int &x){
for (ok=,ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=;
for (x=;isdigit(ch);x=x*+ch-'',ch=getchar());
if (ok) x=-x;
}
int n,m,q,a,b,d;
int out[][maxn],in[maxn][],list[maxn],ans;
struct Matrix{
int v[maxn][maxn];
void init(int op){
for (int i=;i<=m;i++) for (int j=;j<=m;j++) v[i][j]=(i==j)*op;
}
}base,I,emp,tmp,res,sum,last;
Matrix operator+(const Matrix &a,const Matrix &b){
static Matrix c;
c=emp;
for (int i=;i<=m;i++) for (int j=;j<=m;j++) c.v[i][j]=(a.v[i][j]+b.v[i][j])%mod;
return c;
}
Matrix operator*(const Matrix &a,const Matrix &b){
static Matrix c;
c=emp;
for (int i=;i<=m;i++) for (int j=;j<=m;j++) for (int k=;k<=m;k++)
c.v[i][k]=(c.v[i][k]+1LL*a.v[i][j]*b.v[j][k])%mod;
return c;
}
void solve(int n){//1+a+a^2+a^3+...+a^n
if (n<){res=emp;return;}
Matrix a=base,b=a,v=I,s=I;
for(int i=n;i;i>>=,b=b*(a+I),a=a*a)
if(i&) s=s+b*v,v=v*a;
res=s;
}
int main(){
read(n),read(m),I.init(),emp.init();
for (int i=;i<=n;i++){
for (int j=;j<=m;j++) read(out[i][j]);
for (int j=;j<=m;j++) read(in[j][i]);
}
for (int i=;i<=m;i++) for (int j=;j<=m;j++) base.v[i][j]=;
for (int i=;i<=m;i++) for (int j=;j<=n;j++) for (int k=;k<=m;k++)
base.v[i][k]=(base.v[i][k]+1LL*in[i][j]*out[j][k])%mod;
read(q);
while (q--){
read(a),read(b),read(d);
solve(d-);
for (int i=;i<=m;i++) list[i]=;
for (int i=;i<=m;i++) for (int j=;j<=m;j++) list[j]=(list[j]+1LL*out[a][i]*res.v[i][j])%mod;
ans=;
for (int i=;i<=m;i++) ans=(ans+1LL*list[i]*in[i][b])%mod;
printf("%d\n",ans+(a==b));
}
return ;
}
bzoj3583: 杰杰的女性朋友 && 4362: Graph的更多相关文章
- bzoj3583 杰杰的女性朋友 || bzoj4362 Graph
http://210.33.19.103/problem/2174 很显然是矩阵快速幂的题,设有in和ou矩阵,设in矩阵的转置为in' 显然可以直接暴力求出任意两点间走一步路径条数,然后求其d次幂, ...
- [BZOJ3583]杰杰的女性朋友(矩阵快速幂)
杰杰的女性朋友 时间限制:10s 空间限制:256MB 题目描述 杰杰是魔法界的一名传奇人物.他对魔法具有深刻的洞察力,惊人的领悟力,以及令人叹为观止的创造力.自从他从事魔法竞赛以来,短短几 ...
- BZOJ3583 杰杰的女性朋友 矩阵
原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ3583.html 题目传送门 - BZOJ3583 题意 有一个 $n$ 个点构成的有向图. 对于每一个 ...
- BZOJ3583 : 杰杰的女性朋友
将$I$转置,设$G=OI$,则$ans=G^0+G^1+...+G^d$. 注意到$G^d=O(IO)^{d-1}I$,而$IO$是大小为$k\times k$的矩阵,可以通过倍增在$O(k^3\l ...
- 复旦大学EWP菁英女性课程(复旦卓越女性课程改版后第一期) _复旦大学、女性课程、高级研修班、心理学、EWP_培训通课程
复旦大学EWP菁英女性课程(复旦卓越女性课程改版后第一期) _复旦大学.女性课程.高级研修班.心理学.EWP_培训通课程 复旦大学EWP菁英女性课程(复旦卓越女性课程改版后第一期) 学 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- cojs QAQ的矩阵 题解报告
题目描述非常的清晰 首先我们考虑(A*B)^m的求法,这个部分可以参考BZOJ 杰杰的女性朋友 我们不难发现(A*B)^m=A*(B*A)^(m-1)*B A*B是n*n的矩阵,而B*A是k*k的矩阵 ...
- qbxt五一数学Day1
目录 I. 基础知识 1. 带余除法(小学) 1. 定义 2. 性质 2. 最大公约数(gcd)/ 最小公倍数(lcm) 1. 定义 2. 性质 3. 高精度 II. 矩阵及其应用 1. 定义 2. ...
- DOM实战
作者声明:本博客中所写的文章,都是博主自学过程的笔记,参考了很多的学习资料,学习资料和笔记会注明出处,所有的内容都以交流学习为主.有不正确的地方,欢迎批评指正 视频来源:https://www.bil ...
随机推荐
- sql Server 发送邮件 错误类型及原因
设置警报 在[常规项]中做以下设置 新建警报 设置警报名称 选择数据库 选择严重性 在[响应项]中可以做以下设置 选择要邮件通知的操作员 可以设置执行一个警报作业 在选项 ...
- November 4th Week 45th Friday 2016
Problems are not stop signs, they are guidelines. 问题不是休止符,而是指向标. Most of the problems can be overcom ...
- zoj 3657 策略题 easy
http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=4880 由于是要去牡丹江.是浙大出题,所以找了份浙大的题,第一道水题做的就不顺 ...
- 树莓派安装mjpg-streamer视频监控 分类: Raspberry Pi 2015-04-12 23:41 144人阅读 评论(0) 收藏
原来使用Motion在树莓派上跑1280x720分辨率的三颗摄像头.占用内存太严重,关闭诸多功能之后还是不行.故转战mjpg-streamer. 首先安装所需软件 sudo apt-get insta ...
- 内部开发环境 git代码托管说明
前言 随着员工的增加 代码的开发版本管理 提上了日程.初步计划: 1 一台机器 192.168.1.240 git代码版本管理 2 运行环境 192.168.1.241 内部开发运行环境 软件工程师 ...
- android 32 Gallery:横着滚动的列表
Gallery:横着滚动的列表 mainActivity.java package com.sxt.day05_01; import java.util.ArrayList; import java. ...
- React Links
Tutorial: https://hulufei.gitbooks.io/react-tutorial/content/jsx-in-depth.html
- [转] CSS transition
https://css-tricks.com/almanac/properties/t/transition/ The transition property is a shorthand prope ...
- openmpi+NFS+NIS搭建分布式计算集群
1. 配置防火墙 正确配置防火墙的过滤规则,否则会造成NFS文件系统的挂载失败,NIS账户认证的失败,mpirun远程任务实例投放的失败.一般情况下,计算集群是在内部局域网中使用,所以可 ...
- Uncaught SecurityError: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.
canvas绘制图片,由于浏览器的安全考虑,如果在使用canvas绘图的过程中,使用到了外域的图片资源,那么在toDataURL()时会抛出安全异常: Uncaught SecurityError: ...