Cerinta completa

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 4×1 and 1×4 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 :

image

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

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

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

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 , the number of test cases.
Each of the next lines contains an integer , the length of the wall.

Constraints

Output Format

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

Sample Input

2
1
7

Sample Output

0
3

Explanation

For , the brick can be laid in 1 format only: vertically.

The number of primes is .

For , one of the ways in which we can lay the bricks is

Red John

There are ways of arranging the bricks for and there are primes .


Limbajul de programare folosit: java8

Cod:

import java.io.*;
import java.util.*;

public class Solution {
    static int[] primeCountUpTo(int n) {
        boolean[] isPrime = new boolean[n + 1];
        Arrays.fill(isPrime, true);
        if (n >= 0) isPrime[0] = false;
        if (n >= 1) isPrime[1] = false;
        for (int p = 2; p * p <= n; p++) {
            if (isPrime[p]) {
                for (int x = p * p; x <= n; x += p) isPrime[x] = false;
            }
        }
        int[] pref = new int[n + 1];
        for (int i = 1; i <= n; i++) pref[i] = pref[i - 1] + (isPrime[i] ? 1 : 0);
        return pref;
    }

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(br.readLine().trim());
        int[] ns = new int[t];
        int maxN = 0;
        for (int i = 0; i < t; i++) {
            ns[i] = Integer.parseInt(br.readLine().trim());
            maxN = Math.max(maxN, ns[i]);
        }

        int[] ways = new int[Math.max(4, maxN) + 1];
        ways[0] = 1;
        ways[1] = 1;
        ways[2] = 1;
        ways[3] = 1;
        for (int i = 4; i <= maxN; i++) ways[i] = ways[i - 1] + ways[i - 4];

        int maxWays = 0;
        for (int n : ns) maxWays = Math.max(maxWays, ways[n]);
        int[] primePref = primeCountUpTo(maxWays);

        StringBuilder out = new StringBuilder();
        for (int i = 0; i < t; i++) {
            out.append(primePref[ways[ns[i]]]);
            if (i + 1 < t) out.append("\n");
        }
        System.out.print(out.toString());
    }
}

Scor obtinut: 1.0

Submission ID: 464610327

Link challenge: https://www.hackerrank.com/challenges/red-john-is-back/problem

Red John is Back