Codeforces Gym101341I:Matrix God(随机化构造矩阵降维)***
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(随机化构造矩阵降维)***的更多相关文章
- 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. ...
- hdu 5015 233 Matrix(构造矩阵)
http://acm.hdu.edu.cn/showproblem.php?pid=5015 由于是个二维的递推式,当时没有想到能够这样构造矩阵.从列上看,当前这一列都是由前一列递推得到.依据这一点来 ...
- 构造矩阵解决这个问题 【nyoj299 Matrix Power Series】
矩阵的又一个新使用方法,构造矩阵进行高速幂. 比方拿 nyoj299 Matrix Power Series 来说 给出这样一个递推式: S = A + A2 + A3 + - + Ak. 让你求s. ...
- UVa 11149 Power of Matrix (矩阵快速幂,倍增法或构造矩阵)
题意:求A + A^2 + A^3 + ... + A^m. 析:主要是两种方式,第一种是倍增法,把A + A^2 + A^3 + ... + A^m,拆成两部分,一部分是(E + A^(m/2))( ...
- UVA 11149 Power of Matrix 构造矩阵
题目大意:意思就是让求A(A是矩阵)+A2+A3+A4+A5+A6+······+AK,其中矩阵范围n<=40,k<=1000000. 解题思路:由于k的取值范围很大,所以很自然地想到了二 ...
- Number Sequence(HDU 1005 构造矩阵 )
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- Codeforces 392C Yet Another Number Sequence (矩阵快速幂+二项式展开)
题意:已知斐波那契数列fib(i) , 给你n 和 k , 求∑fib(i)*ik (1<=i<=n) 思路:不得不说,这道题很有意思,首先我们根据以往得出的一个经验,当我们遇到 X^k ...
- CodeForces 450B Jzzhu and Sequences (矩阵优化)
CodeForces 450B Jzzhu and Sequences (矩阵优化) Description Jzzhu has invented a kind of sequences, they ...
- poj 3735 Training little cats(构造矩阵)
http://poj.org/problem?id=3735 大致题意: 有n仅仅猫,開始时每仅仅猫有花生0颗,现有一组操作,由以下三个中的k个操作组成: 1. g i 给i仅仅猫一颗花生米 2. e ...
随机推荐
- WPF版的HideCaret()
原文:WPF版的HideCaret() WPF版的HideCaret() 周银辉 事情是这样的: 一般说来,对于那些拥有句柄的TextBox(RichTextBox同理)控件,比如win32的,Win ...
- thinkphp 删除所有缓存 Rumtime 以及 Html 静态缓存
<?php /** * This is not a free software, All Copyright @F.Z.B * Date: 14-8-12 下午4:08 * File: Cach ...
- 关于 Swift 中的 Array.contains 方法
Swift 2.0 中对语言进行了又一次的改进,这次将整个语言变得更加面向对象化,比如在 Swift 1.x 中如果要判断某个元素是否在数组中,就需要用到 contains 函数: if contai ...
- Emgu-WPF学习使用-Rectangle识别
原文:Emgu-WPF学习使用-Rectangle识别 环境:Win8 64位 Vs2015 Emgu 版本:emgucv-windesktop 3.2.0.2682 示例图上部流程:原图->灰 ...
- WPF 多点触摸开发[2]:WPF触摸的几个手势的执行顺序
原文:WPF 多点触摸开发[2]:WPF触摸的几个手势的执行顺序 前面我讲了在win7下使用模拟器,进行调试模拟多点触摸,其实际开发中这样也比较麻烦.. 要拿几个鼠标. 所以更多的人会 买个触摸套 套 ...
- jquery QQ微博
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- boost库asio详解8——几个TCP的简单例子
摘于boost官网的几个例子, 做了点小修改, 笔记之. 同步客户端 void test_asio_synclient() { typedef boost::asio::io_service IoSe ...
- js onload事件使用
<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...
- Z-Order
The z-order of a window indicates the window's position in a stack of overlapping windows. This wind ...
- 字符串、数组操作函数 Copy Concat Delete Insert High MidStr Pos SetLength StrPCopy TrimLeft
对字符串及数组的操作,是每个程序员必须要掌握的.熟练的使用这些函数,在编程时能更加得心应手. 1.Copy 功能说明:该函数用于从字符串中复制指定范围中的字符.该函数有3个参数.第一个参数是数据源(即 ...