Red John is Back


Problem Statement :


Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows.

There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where 1 <= n <= 4:

image

There is one more step to the puzzle. Call the number of possible arrangements M. Patrick must calculate the number of prime numbers P in the inclusive range 0-M.

As an example, assume n=3. From the diagram above, we determine that there is only one configuration that will cover the wall properly. 1 is not a prime number, so P=0.

A more complex example is n = 5. The bricks can be oriented in  total configurations that cover the wall. The two primes 2 and 3 are less than or equal to 3, so P =2.

image

Function Description

Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer.

redJohn has the following parameter(s):

n: an integer that denotes the length of the wall

Input Format

The first line contains the integer t, the number of test cases.
Each of the next t lines contains an integer n, the length of the 4*n wall.

Constraints
1 <= t <= 20
1 <= n <= 40

Output Format

Print the integer P on a separate line for each test case.



Solution :



title-img


                            Solution in C :

In C++ :





#include <iostream>
#define FOR(i,a)    for(int i = 0;i < (a);i++)
#define REP(i,a,b)  for(int i = (a);i < (b);i++)
#define SZ(a)   ((signed)a.size())
#define PB(a)   push_back(a)
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <cstdio>

using namespace std;

int l[1000001],k = 0;
bool prime[1000001] = {0};

void preprocess(){
    for(long long i = 2;i < 1000001;i++){
        if(!prime[i]){
            l[k++] = i;
            for(long long j = i*i ; j <= 1000000;j += i)    prime[j] = 1;
        }
    }
}

void solve(){
    long long arr[101];
    arr[1] = 1,arr[2] = 1,arr[3] = 1,arr[4] = 2;
    arr[0] = 1;
    REP(i,5,41)    arr[i] = (arr[i - 1] + arr[i - 4]);
    int c = 0;
    int v;
    cin>>v;
    FOR(i,k){
        if(l[i] <= arr[v])  c++;
        else    break;
    }
    cout<<c<<"\n";
}

int main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int t;
    preprocess();
    cin >> t;
    FOR(i,t)    solve();
    return 0;
}








In Java :





import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * User: Sandesh
 * Date: 7/27/13
 * Time: 10:47 PM
 * To change this template use File | Settings | File Templates.
 */
public class Solution {
    static long dp[];
    static HashSet<Integer> hs;
    public static void main(String arg[]){
        hs=new HashSet<Integer>();
        dp=new long[41];
        Arrays.fill(dp,-1);
        Scanner sc=new Scanner(System.in);
        int N=sc.nextInt();
        setPrime();
        for (int j=0;j<N;j++){
            int val=sc.nextInt();
            int x=(int)(new Solution().get(val));
            int ans=0;
            for (int i=2;i<=x;i++){
                if(hs.contains(i))
                    ans++;
            }
            System.out.println(ans);
        }
    }
    public long get(int N){
        if(dp[N]!=-1)
            return dp[N];
        long ans=1;
        if(N<=3)
            ans=1;
        else {
            ans=get(N-1)+get(N-4);
        }
        dp[N]=ans;
        return ans;
    }
    static boolean isPrime(int a){
        for (int i=2;i*i<=a;i++){
            if(a%i==0)
                return false;
        }
        return true;
    }
    static void setPrime(){
        for (int i=2;i<=217286;i++){
            if(isPrime(i))
                hs.add(i);
        }

    }

}








In C :





#include<stdio.h>
#include<stdlib.h>
  int sieve[300000][2];
int main()
{
    int t,i,j,k,n;
    int a[41],x;
    a[1]=1;
    a[0]=0;
    a[2]=1;
    a[3]=1;
    a[4]=2;
    for(i=5;i<=40;i++)
    {
        a[i]=a[i-1]+a[i-4];
    }
   // printf("%d\n",a[40]);
    for(i=0;i<300000;i++)
    {
        for(j=0;j<2;j++)
        sieve[i][j]=0;
    }
    for(i=2;i<300000;i++)
    {
        if(sieve[i][0]==0)
         {
             sieve[i][1]=sieve[i-1][1]+1;
         }
         else
         {
             sieve[i][1]=sieve[i-1][1];
             continue;
         }
        for(j=2*i;j<300000;j=j+i)
        {
            sieve[j][0]=1;
        }
    }
    scanf("%d",&t);
    for(i=0;i<t;i++)
    {
        scanf("%d",&n);
        printf("%d\n",sieve[a[n]][1]);
    }

    return(0);
}








In Python3 :





nbCombMem = ([],[])
def nbComb(N) :
   if N in nbCombMem[0] :
      return nbCombMem[1][nbCombMem[0].index(N)]
   if N < 0 : return 0
   c = 1
   for i in range(0,N-3) :
      c += nbComb(N-4-i)
   nbCombMem[0].append(N)
   nbCombMem[1].append(c)
   return c
    
# return the primes (strictly) under n. Use a sieve of erathostene.
def getPrimesUnder(n) :
   r = [2]
   n2 = n // 2
   l = list(True for i in range(0,n2))
   l[0] = False
   for i in range(1,n2) :
      if l[i] :
         r.append(2*i+1)
         for m in range(2*i*(i+1),n2,2*i+1) :
            l[m] = False
   return r
        
# main
T = int(input())
Cs = [nbComb(int(input())) for t in range(0,T)]

Ps = getPrimesUnder(max(Cs)+1)

for t in range(0,T) :
    c = 0
    while c < len(Ps) and Ps[c] <= Cs[t] : c += 1
    print(c)
                        








View More Similar Problems

Delete a Node

Delete the node at a given position in a linked list and return a reference to the head node. The head is at position 0. The list may be empty after you delete the node. In that case, return a null value. Example: list=0->1->2->3 position=2 After removing the node at position 2, list'= 0->1->-3. Function Description: Complete the deleteNode function in the editor below. deleteNo

View Solution →

Print in Reverse

Given a pointer to the head of a singly-linked list, print each data value from the reversed list. If the given list is empty, do not print anything. Example head* refers to the linked list with data values 1->2->3->Null Print the following: 3 2 1 Function Description: Complete the reversePrint function in the editor below. reversePrint has the following parameters: Sing

View Solution →

Reverse a linked list

Given the pointer to the head node of a linked list, change the next pointers of the nodes so that their order is reversed. The head pointer given may be null meaning that the initial list is empty. Example: head references the list 1->2->3->Null. Manipulate the next pointers of each node in place and return head, now referencing the head of the list 3->2->1->Null. Function Descriptio

View Solution →

Compare two linked lists

You’re given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. If all data attributes are equal and the lists are the same length, return 1. Otherwise, return 0. Example: list1=1->2->3->Null list2=1->2->3->4->Null The two lists have equal data attributes for the first 3 nodes. list2 is longer, though, so the lis

View Solution →

Merge two sorted linked lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the heads of two sorted linked lists, merge them into a single, sorted linked list. Either head pointer may be null meaning that the corresponding list is empty. Example headA refers to 1 -> 3 -> 7 -> NULL headB refers to 1 -> 2 -> NULL The new list is 1 -> 1 -> 2 -> 3 -> 7 -> NULL. Function Description C

View Solution →

Get Node Value

This challenge is part of a tutorial track by MyCodeSchool Given a pointer to the head of a linked list and a specific position, determine the data value at that position. Count backwards from the tail node. The tail is at postion 0, its parent is at 1 and so on. Example head refers to 3 -> 2 -> 1 -> 0 -> NULL positionFromTail = 2 Each of the data values matches its distance from the t

View Solution →