Judgement Day

Skynet has figured out a way to take over the world. It will keep producing robots and molecular assemblers until it has converted all matter for its own purpose. Robots work to produce more robots and molecular assemblers. Molecular assemblers convert matter into programmable matter to be used to produce more robots and molecular assemblers.

Initial Info On day 0, Skynet started with 3 robots, 1 molecular assembler and 0 units of programmable matter.

On day i (i>0), robots produced can be calculated as the sum of the robots produced on the previous day and thrice the units of programmable matter produced on the previous day.

On day i, number of molecular assemblers produced can be calculated as the sum of robots produced on the previous day and twice the units of programmable matter produced on the previous day.

On day i, the units of programmable matter produced can be calculated as five times the number of molecular assemblers produced on the previous day.

How many robots, molecular assemblers and units of programmable matter will be produced on the Judgement Day (day n)?

Input/Output Specifications

Input format: Two integers x and y (1<=x,y<=10^6) such that the judgement day falls on day n = x*y.

Output format: Since the number of robots, molecular assemblers and units of programmable matter grows very rapidly, we want you to output them modulo 1000000007.

The output should be a string having the following format: R#M#P, where - R is the number of robots produced on judgement day modulo 1000000007 - M is the number of molecular assemblers produced on judgement day modulo 1000000007 - P is the number of units of programmable matter produced on judgement day modulo 1000000007

I have coded it but I think my code takes much more time.

- Tushar Arora September 17, 2014Can any one of you please suggest a much more optimized code/algorithm for the above problem

{class CandidateCode

{

public static String JudgementDay(int input1,int input2)

{

//Write code here

long r=3;//initial number of robots

long ma=1;//initial number of molecular assemblers

long pm=0;//initial units of programmable matter

long newr;//variable to store new number of robots

long newma;//variable to store new number of molecular assemblers

long newpm;//variable to store new units of programmable matter

long modu=1000000007;//modulus

for(int i=1;i<=input1;i++)

for(int j=1;j<=input2;j++)

{

//this loop will get executed n times where n is the judgement day(from //day 1 to day n)

newr=r+3*pm;//calculating new number of robots

newma=r+2*pm;//calculating new number of molecular assemblers

newpm=5*ma;//calculating new unit of programmable matter

r=newr%modu;

ma=newma%modu;

pm=newpm%modu;

}

String str=""+r+"#"+ma+"#"+pm;

return str;

}

}}