1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
// Input
// First line consists of N, the dimension of matrix.
// Each of the next N lines contains N space separated integers. This is matrix A.
// Each of the next N lines contains N space separated integers. This is matrix B.
// Next line contains Q, the number of queries asked by Sid.
// Each of the next Q lines consists of queries of the form
// “A i j K” or “B i j K” (quotes for clarity), meaning
// change the element in ith row and jth column of matrix A or B to value K.
//
// Output
// Output exactly Q lines corresponding to Q queries, each containing the sum of
// the elements of the matrix A*B.
//
// Example
// Input:
// 2
// 1 2
// 3 4
// 4 3
// 2 1
// 3
// A 1 1 2
// B 0 1 3
// A 0 0 10
//
// Output:
// 40
// 40
// 103
//
// Constraints:
// 1<=N<=100
// 1<=Q<=100000
// 0<=i,j<N
// -10^6 <= A[i][j], B[i][j] <= 10^6
#include <chrono>
#include <iomanip> // pointless in production code
#include <iostream>
#include <random>
#include <vector>
using matrix = std::vector<std::vector<int>>;
int getRndInt(int min, int max);
int getRndChar();
matrix resizedMatrix(std::size_t size);
void fillMatrixRndInts(matrix & arr);
void printMatrix(const matrix & m);
long long int matrixSum(matrix & arrA, matrix & arrB);
int main()
{
int N { getRndInt(2, 4) }; // constraint: 1 <= N <= 100
std::cout << "input: " << N << '\n';
matrix arrA { resizedMatrix(N) },
arrB { resizedMatrix(N) };
fillMatrixRndInts(arrA);
fillMatrixRndInts(arrB);
unsigned int Q = getRndInt(1, 4); // costraint: 1 <= Q <= 100,000
std::cout << "\nqueries: " << Q << '\n';
while(Q--) {
char ch = getRndChar();
int x { getRndInt(1, N-1) },
y { getRndInt(1, N-1) };
long long int z { getRndInt(1, 100) };
if(ch == 'A') { arrA[x][y] = z; }
else { arrB[x][y] = z; }
std::cout << ch << ' ' << x << ' ' << y << ' ' << z
<< " --> matrixSum(arrA, arrB): " << matrixSum(arrA, arrB) << '\n';
}
return 0;
}
matrix resizedMatrix(std::size_t size)
{
matrix m(size, std::vector<int>(size));
return m;
}
void fillMatrixRndInts(matrix & arr)
{
for(auto& v : arr) {
for(int& i : v) {
i = getRndInt(1, 100); // contraint: -10^6 <= A[i][j], B[i][j] <= 10^6
}
}
printMatrix(arr);
return;
}
int getRndInt(int min, int max)
{
static std::mt19937 eng {
static_cast<unsigned>( std::chrono::high_resolution_clock::now()
.time_since_epoch()
.count() )
};
std::uniform_int_distribution<> dst(min, max);
return dst(eng);
}
void printMatrix(const matrix & m)
{
std::cout << "\nmatrix:\n";
for(const auto& v : m) {
for(const int i : v) {
std::cout << std::setw(4) << i; // std::cout << i << ' ';
}
std::cout << '\n';
}
}
int getRndChar()
{
static std::mt19937 eng {
static_cast<unsigned>( std::chrono::high_resolution_clock::now()
.time_since_epoch()
.count() )
};
std::uniform_int_distribution<char> dst('A', 'B');
return dst(eng);
}
long long int matrixSum(matrix & arrA, matrix & arrB)
{
long long int sum2 = 0;
for(size_t i=0; i < arrA.size(); ++i) {
long long int sum = 0;
long long int sum1 = 0;
for(size_t j=0; j<arrA.size(); j++) {
sum += arrA[j][i];
sum1 += arrB[i][j];
}
sum2 += sum * sum1;
}
return sum2;
}
|