We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
My java code passed the first case but "Termited due to Timeout" in other.
Can Anyone help me in optimizing the code ?
importjava.io.*;importjava.util.*;importjava.math.*;publicclassSolution{staticBigIntegerpermutation(BigIntegern,BigIntegerk){BigIntegerresult=BigInteger.ONE;for(BigIntegerbi=BigInteger.valueOf(1);bi.compareTo(k)<=0;bi=bi.add(BigInteger.ONE)){result=result.multiply(n);result=result.divide(bi);n=n.subtract(BigInteger.ONE);}//System.out.println("Last : "+result);returnresult;}publicstaticvoidmain(String[]args){Scannersc=newScanner(System.in);inttestCases=sc.nextInt();while(testCases-->0){BigIntegertotal;total=sc.nextBigInteger();BigIntegersum=BigInteger.ZERO;// for(int block =2 ; block<=4 ;block++)for(BigIntegerblock=BigInteger.valueOf(2);block.compareTo(BigInteger.valueOf(4))<=0;block=block.add(BigInteger.ONE)){BigIntegermaxBlock=total.divide(block);for(BigIntegercolor=BigInteger.valueOf(1);color.compareTo(maxBlock)<=0;color=color.add(BigInteger.ONE)){BigIntegerblack=total.subtract(color.multiply(block));BigIntegertiles=black.add(color);BigIntegercombinations=permutation(tiles,color);sum=sum.add(combinations);sum=sum.mod(BigInteger.valueOf(1000000007));}}System.out.println(sum);}/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #116: Red, green or blue tiles
You are viewing a single comment's thread. Return to all comments →
My java code passed the first case but "Termited due to Timeout" in other.
Can Anyone help me in optimizing the code ?