 # MULTIMATRIX Editorial

Practice

Author: Prathamesh Sogale
Author: Prathamesh Sogale
Editorialist: Yogesh Deolalkar

Simple.

Math

# PROBLEM:

Yash is working on his final year project, he decided to build an image encryption technique. He found that best way to represent an image is in a form of a matrix. He tried to implement some hashing techniques to encrypt the images but when he tried to decrypt those images he found out that only some special types of matrices can be decrypted.Time is very less and he don’t have more time to fix this issue or to start new project. So he decided to submit his project with only the images which satisfy some special conditions. Conditions are as follows. You can perform matrix multiplication any number of times but the resultant matrix should be same as original matrix.

You are given an integer NN denoting size of matrix, And an N∗NN∗N matrix you need to determine if it satisfies the above conditions if it is possible Print “YES” else Print “NO”.

# SOLUTIONS:

Setter's Solution

#include
using namespace std;
using ll = long long;

string getAns(ll **a,ll **b,ll n){

``````for(ll i = 0; i < n; i++){
for(ll j = 0; j < n; j++){
ll ans = 0;
for(ll k = 0; k < n; k++){
ans += a[i][k] * b[k][j];
}
if(ans != a[i][j]){
return "NO";
}
}
}
return "YES";
``````

}
int main() {
ll t;
cin>>t;
while(t–){
ll n;
cin>>n;
ll** a = new ll*[n];

``````     for(ll i=0;i<n;i++){
a[i] = new ll[n];
for(ll j=0;j<n;j++){
cin>>a[i][j];
}
}

cout<<getAns(a,a,n)<<endl;
}
return 0;
``````

}

Tester's Solution

for i in range(int(input())):
n=int(input())
list1=[]
for i in range(n):
temp=list(map(int,input().split()))
list1.append(temp)
result=True
for i in range(n):
for j in range(n):
ans=0
for k in range(n):
ans +=list1[i][k]*list1[k][j]
if ans !=list1[i][j]:
result=False
if result:
print(“YES”)
else:
print(“NO”)