Project Euler #116: Red, green or blue tiles

Sort by

recency

|

10 Discussions

|

  • + 0 comments

    It passed 100% using linear recurrence and matrix exponentiation in O(log(n)) Red: f(n) = f(n-1) + f(n-2) + 1 Green: f(n) = f(n-1) + f(n-3) + 1 Blue: f(n) = f(n-1) + f(n-4) + 1

    to reduce the time %(10**+7) must be added inside the matrix exponentiation

  • + 0 comments

    I get runtimeError with all cases but case 0 and 0 points but ... my python code solves the original Euler problem test case.

  • + 0 comments

    Swift. [Segmentation Fault]

    ~Don't know what to do..

    var factorials = [1:1]
    func factorial(n: Int) -> Int{
        if let fact = factorials[n]{
            return fact
        }
        if n > 0{
            let r = n*factorial(n: n-1)
            factorials[n] = r
            return r
        }
        return 1
    }
    func getTiles(n: Int, m: Int) -> Int{
        var result = 0
        for i in 1...n/m{
            let tile = i
            let empty = n-(i*m)
            let sum = tile+empty
            result += factorial(n: sum)/(factorial(n: tile)*factorial(n: empty))
        }
        return result
    }
    func ways(n: Int) -> Int{
        return getTiles(n: n, m: 2)+getTiles(n: n, m: 3)+getTiles(n: n, m: 4)
    }
    
  • + 0 comments

    My java code passed the first case but "Termited due to Timeout" in other.

    Can Anyone help me in optimizing the code ?

    import java.io.*;
    import java.util.*;
    import java.math.*;
    
    public class Solution 
    {
        static BigInteger permutation(BigInteger n , BigInteger k)
        {
            BigInteger result = BigInteger.ONE;
            for (BigInteger bi = 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);
            return result;
    
        }
        public static void main(String[] args) 
        {
            Scanner sc = new Scanner(System.in);        
            int testCases = sc.nextInt();
            while(testCases-- >0)
            {
                BigInteger total ;
                total = sc.nextBigInteger();
                BigInteger sum = BigInteger.ZERO;
    //            for(int block =2 ; block<=4 ;block++)
                for(BigInteger block =BigInteger.valueOf(2) ; block.compareTo(BigInteger.valueOf(4))<=0 ;block = block.add(BigInteger.ONE))
                {   
                    BigInteger maxBlock = total.divide(block);
    
                    for(BigInteger color = BigInteger.valueOf(1); color.compareTo(maxBlock) <= 0; color = color.add(BigInteger.ONE))                               {
                      BigInteger black = total.subtract(color.multiply(block));
                      BigInteger tiles = black.add(color);
                      BigInteger combinations = 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. */
        }
    }
    
  • + 1 comment
    import math
    def C(n,r):
        f = math.factorial
        return f(n) // f(r) // f(n-r)
    
    t=int(input())
    for i in range(t):
        n=int(input())
        r=g=b=0
        for rr in range(1,(n//2)+1):
            r+=C(n-rr,rr)
        for gg in range(1,(n//3)+1):
            g+=C(n-(2*gg),gg)
        for bb in range(1,(n//4)+1):
            b+=C(n-(3*bb),bb)
        print((r+g+b)%1000000007)
    

    I think it take less time with compare to next (;一_一) but nope, it's take 8.33% more loop than 0-n

    for i in range(int(input())):
        a=b=m=n=o=w=x=y=z=1
        for j in range(1,int(input())):
            a,b,m,n,o,w,x,y,z = b,a+b,n,o,o+m,x,y,z,z+w
        print((b+n+x-3)%1000000007)
    

    smallest in size (lines of code)?

    but still TLE ಠ_ಥ