# Hardcoding matrix multiplication

I was doing a problem, SPOJ: FLIB. My code was running slow (getting Time Limit Exceeded error) when I was using Code1.

However, my code accepted within the required time limit when I hardcoded the multiplication instead, using Code2. Can someone tell how this was a significant change? Note that this program relies heavily on multiplication, and it is the performed repeatedly.

The question is on matrix exponentiation, and multiplication of two matrices is a very crucial aspect in it.

A and B are two matrices which are multiplied, and whose result is stored in C. M is an int with which I am supposed to take the modulus. M = 10^9 + 7. s is of type long long, and I have used to it to reduce the number of times I have to apply the modulus operator.

Each element in these matrices can be of the order of $10^9$, after taking the modulus.

Code1:

for(i=0;i<3;i++)
for(j=0;j<3;j++)
C[i][j] = 0;

for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
s=0;
for(k=0;k<3;k++)
s += ((long long)(A[i][k]))*B[k][j];
s = s%M;
C[i][j] = s;
}


Code2:

C= (((long long)A)*B + ((long long)A)*B + ((long long)A)*B)%M;
C= (((long long)A)*B + ((long long)A)*B + ((long long)A)*B)%M;
C= (((long long)A)*B + ((long long)A)*B + ((long long)A)*B)%M;
C= (((long long)A)*B + ((long long)A)*B + ((long long)A)*B)%M;
C= (((long long)A)*B + ((long long)A)*B + ((long long)A)*B)%M;
C= (((long long)A)*B + ((long long)A)*B + ((long long)A)*B)%M;
C= (((long long)A)*B + ((long long)A)*B + ((long long)A)*B)%M;
C= (((long long)A)*B + ((long long)A)*B + ((long long)A)*B)%M;
C= (((long long)A)*B + ((long long)A)*B + ((long long)A)*B)%M;


This is the complete code:

#include <bits/stdc++.h>
using namespace std;

#define M 1000000007

void matmult(int A, int B, int C)
{

C= (((long long)A)*B + ((long long)A)*B + ((long long)A)*B)%M;
C= (((long long)A)*B + ((long long)A)*B + ((long long)A)*B)%M;
C= (((long long)A)*B + ((long long)A)*B + ((long long)A)*B)%M;
C= (((long long)A)*B + ((long long)A)*B + ((long long)A)*B)%M;
C= (((long long)A)*B + ((long long)A)*B + ((long long)A)*B)%M;
C= (((long long)A)*B + ((long long)A)*B + ((long long)A)*B)%M;
C= (((long long)A)*B + ((long long)A)*B + ((long long)A)*B)%M;
C= (((long long)A)*B + ((long long)A)*B + ((long long)A)*B)%M;
C= (((long long)A)*B + ((long long)A)*B + ((long long)A)*B)%M;

}

void matpow(int Z, long long n, int A)
{
int temp;
int i,j;

A = 1;
A = 0;
A = 0;
A = 0;
A = 1;
A = 0;
A = 0;
A = 0;
A = 1;

while(n>0)
{
if(n&1)
{
matmult(A,Z,temp);

for(i=0;i<3;i++)
for(j=0;j<3;j++)
A[i][j] = temp[i][j];
}

matmult(Z, Z, temp);

for(i=0;i<3;i++)
for(j=0;j<3;j++)
Z[i][j] = temp[i][j];

n/=2;
}

}

int main(int argc, const char * argv[])
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);

int t;
long long n,ans = 0;

int A;

cin>>t;

while(t--)
{
cin>>n;

int Z = {{1,2,1},{0,5,3},{0,3,2}};

if(n>1)
{
matpow(Z,n-1,A);

ans = ((long long)(A)*2 + (long long)(A)*5 + (long long)A*3)%M;
}

else
{
if(n==0)
ans = 0;
else if(n==1)
ans = 2;
else if(n==2)
ans = 15;
}

cout<<ans<<"\n";

}

return 0;
}

• Hi. Welcome to Code Review! We tend to prefer questions with more context. In particular, a problem statement explaining what the purpose of the code is (preferably with a link to the original) and the rest of the code. As written, this looks more like a Stack Overflow question with a minimal example. I'd like to know how this code is called, how often, and what data types A, B, C, M, and s are. Also, what are typical values for each? – Brythan Jan 30 '15 at 8:37
• @Brythan: I have edited the question. Please let me know if there is something more I should add. – Dhruv Mullick Jan 30 '15 at 8:58
• I'd strongly recommend posting the rest of the code. Particularly as the linked problem doesn't obviously involve matrix multiplication but Fibonacci calculations. As it is, people can't run the code, so no one can try making changes to see what happens. – Brythan Jan 30 '15 at 9:10
• Added the code. – Dhruv Mullick Jan 30 '15 at 9:45

You also have this little loop in your original code that is not needed.

for(i=0;i<3;i++)
for(j=0;j<3;j++)
C[i][j] = 0;


Note: It is not needed because you have an explicit assignment to each element.

    C[i][j] = s;


So the two pieces of code are not equivalent.

Also you have introduced a bug:

C= (((long long)A)*B + ((long long)A)*B + ((long long)A)*B)%M;


The operator % has a higher precedence than + so your modulus is being applied to the last element only before the addition.

// ie. You have
C = T1 + T2 + (T3 % M);
// You want
C = (T1 + T2 + T3) % M;


Other things:

// Don't do this.
using namespace std;

// Macros have not type information.
#define M 1000000007

// Prefer to use static const
static long long const M = 1000000007;


You may want to initialize t

int t;
cin>>t;    // If the user types in Fred. Then the read will fail
// the value of t is then undefined

while(t--) // This could go for a very long time.


One variable per line pelase.

long long n,ans = 0;


Declare variables as close to the point of use as possible.

    cin>>n;  // n is defined outside the current scope.
// yet not used anywhere but inside the loop


Some white space between identifiers would be nice.

First did you compile with optimizations enabled? If not then do so when profiling. A good optimizer will produce equivalent code after unrolling the loops.

The first double for loop is superfluous. It is doing busy work that is overwritten in the second loop.