http://codeforces.com/gym/101341/problem/I

题意:给三个N*N的矩阵,问a*b是否等于c。

思路:之前遇到过差不多的题目,当时是随机行(点),然后验证,不满足就退出。还有暴力弄的(当时的数据是500)。也提到过这样的解法,当时没用这种做法做一遍。

就是构造多一个矩阵d。

由于矩阵乘法满足结合律:a * (b * d) = c * d. d是一个n*1的矩阵,b * d之后会得到一个n * 1的矩阵,因此只需要O(n^2)就可以验证是否正确。

 #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define N 1010
const int MOD = 1e9 + ;
LL a[N][N], b[N][N], c[N][N];
LL d[N], t0[N], t1[N], t2[N]; int n; int main() {
scanf("%d", &n);
srand(time(NULL));
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++) scanf("%lld", &a[i][j]);
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++) scanf("%lld", &b[i][j]);
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++) scanf("%lld", &c[i][j]);
bool flag = ;
for(int cas = ; cas <= && flag; cas++) {
for(int i = ; i <= n; i++) d[i] = rand() % MOD;
for(int i = ; i <= n; i++) {
t2[i] = ;
for(int j = ; j <= n; j++)
t2[i] = (t2[i] + c[i][j] * d[j]) % MOD;
} for(int i = ; i <= n; i++) {
t1[i] = ;
for(int j = ; j <= n; j++)
t1[i] = (t1[i] + b[i][j] * d[j]) % MOD;
} for(int i = ; i <= n && flag; i++) {
t0[i] = ;
for(int j = ; j <= n; j++)
t0[i] = (t0[i] + a[i][j] * t1[j]) % MOD;
if(t0[i] != t2[i]) flag = ;
}
}
if(flag) puts("YES");
else puts("NO");
return ;
}

Codeforces Gym101341I:Matrix God(随机化构造矩阵降维)***的更多相关文章

  1. POJ 3233 Matrix Power Series(构造矩阵求等比)

    Description Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + … + Ak. ...

  2. hdu 5015 233 Matrix(构造矩阵)

    http://acm.hdu.edu.cn/showproblem.php?pid=5015 由于是个二维的递推式,当时没有想到能够这样构造矩阵.从列上看,当前这一列都是由前一列递推得到.依据这一点来 ...

  3. 构造矩阵解决这个问题 【nyoj299 Matrix Power Series】

    矩阵的又一个新使用方法,构造矩阵进行高速幂. 比方拿 nyoj299 Matrix Power Series 来说 给出这样一个递推式: S = A + A2 + A3 + - + Ak. 让你求s. ...

  4. UVa 11149 Power of Matrix (矩阵快速幂,倍增法或构造矩阵)

    题意:求A + A^2 + A^3 + ... + A^m. 析:主要是两种方式,第一种是倍增法,把A + A^2 + A^3 + ... + A^m,拆成两部分,一部分是(E + A^(m/2))( ...

  5. UVA 11149 Power of Matrix 构造矩阵

    题目大意:意思就是让求A(A是矩阵)+A2+A3+A4+A5+A6+······+AK,其中矩阵范围n<=40,k<=1000000. 解题思路:由于k的取值范围很大,所以很自然地想到了二 ...

  6. Number Sequence(HDU 1005 构造矩阵 )

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  7. Codeforces 392C Yet Another Number Sequence (矩阵快速幂+二项式展开)

    题意:已知斐波那契数列fib(i) , 给你n 和 k , 求∑fib(i)*ik (1<=i<=n) 思路:不得不说,这道题很有意思,首先我们根据以往得出的一个经验,当我们遇到 X^k ...

  8. CodeForces 450B Jzzhu and Sequences (矩阵优化)

    CodeForces 450B Jzzhu and Sequences (矩阵优化) Description Jzzhu has invented a kind of sequences, they ...

  9. poj 3735 Training little cats(构造矩阵)

    http://poj.org/problem?id=3735 大致题意: 有n仅仅猫,開始时每仅仅猫有花生0颗,现有一组操作,由以下三个中的k个操作组成: 1. g i 给i仅仅猫一颗花生米 2. e ...

随机推荐

  1. WPF 属性变更通知类的实现

    原文:WPF 属性变更通知类的实现 平时用依赖属性多一些,普通属性的变更通知知道有这个方法,但是老是忘记名字,再写一遍吧. public class Student : INotifyProperty ...

  2. [转]更改ejs模板后缀.ejs为.html

    三种写法 1,express老写法,3.*已经不支持 app.register('.html', require('ejs')); app.set('view engine', 'ejs'); 2, ...

  3. 线性渐变、辐射渐变、角度渐变-QLinearGradient,QRadialGradient,QConicalGradient

    渐变,是指逐渐的,有规律性的变化,是一种规律性很强的现象.Qt提供了一个与渐变相关的QGradient类,目前支持三种渐变画刷,分别是线性渐变(QLinearGradient).辐射渐变(QRadia ...

  4. MVVM 下 ContextMenu的命令绑定

    原文:MVVM 下 ContextMenu的命令绑定 由于ContextMenu不继承父级的DataContext,所以如果要绑定父级的DataContext,直接DataContext=" ...

  5. HTML5离线缓存攻击测试

    本实验采用局域网模拟,通过修改本地HOSTS文件来模拟域名以及DNS欺骗.合法网站使用Linux CentOS7的apache服务器搭建,IP为192.168.1.113,HOSTS文件中加入192. ...

  6. SQL Server 2016新特性:DROP IF EXISTS

    原文:SQL Server 2016新特性:DROP IF EXISTS  在我们写T-SQL要删除某个对象(表.存储过程等)时,一般会习惯先用IF语句判断该对象是否存在,然后DROP,比如: 旧 ...

  7. sql语句查询重复值

    select * from user where name in (select name from user group by name having count(*)>1)

  8. “流”的5个例子(TStream是抽象类,写到文件里和内存里,都是一样的)

    http://www.cnblogs.com/keyvip/category/270215.html

  9. WPF四年,尤不足以替代WinForm

    WPF四年,尤不足以替代WinForm WPF出山已四年,作为官方内定的下一代UI系统掌门,没少露脸.但这个新掌门能否胜任,仍是众多开发者的心头之虑.通过对VisualStudio 2010的编辑器部 ...

  10. Codility---Brackets

    Task description A string S consisting of N characters is considered to be properly nestedif any of ...