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中Style文件的引用——使用xaml代码或者C#代码动态加载
原文:WPF中Style文件的引用--使用xaml代码或者C#代码动态加载 WPF中控件拥有很多依赖属性(Dependency Property),我们可以通过编写自定义Style文件来控制控件的外观 ...
- .NET Core 3.0 Preview 5 亮点之一:发布单文件可执行程序
在阅读 Announcing .NET Core 3.0 Preview 5 时发现了 .NET Core 3.0 Preview 5 的一个新特性 —— Publishing Single EXEs ...
- C# 异步委托回调函数使用
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- 图像滤镜艺术---ZPhotoEngine超级算法库
原文:图像滤镜艺术---ZPhotoEngine超级算法库 一直以来,都有个想法,想要做一个属于自己的图像算法库,这个想法,在经过了几个月的努力之后,终于诞生了,这就是ZPhotoEngine算法库. ...
- Win8 Metro(C#)数字图像处理--2.51图像统计滤波算法
原文:Win8 Metro(C#)数字图像处理--2.51图像统计滤波算法 [函数名称] 图像统计滤波 WriteableBitmap StatisticalFilter(Writeab ...
- RSA加密解密及RSA签名和验证
原文:RSA加密解密及RSA签名和验证 1.RSA加密解密: (1)获取密钥,这里是产生密钥,实际应用中可以从各种存储介质上读取密钥 (2)加密 (3)解密2.RSA签名和验证 (1)获取密钥,这里是 ...
- C# string.Format()格式
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...
- C语言的setlocale和localtime函数(C++也可用)
Example 1234567891011121314151617181920212223242526272829303132 /* setlocale example */ #include < ...
- 百度网盘背后的存储系统atlas
原文 http://www.bitstech.net/2015/07/25/baidu-atlas/ 百度网盘免费提供2TB存储, 它的存储量一定是惊人的, 支持它的存储系统atlas也是相当不 ...
- 全部的Windows消息对应值
以下是全部的Windows消息, 对于未在MSDN上的消息的WPARAM, LPARAM参数解释正确的给分 [已知 :0x0313, 0x01e2, 0x01e5, 0x01e ...