PPBRJB - Red John is Back

no tags 

Red John has committed another murder. But this time, he doesn't leave a red smiley behind. What he leaves behind is 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 where. The victim also 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 in the wall. In every configuration, the wall has to be completely covered using the bricks. There is a phone number written on a note in the safe which is of utmost importance in the murder case. Gale Bertram wants to know the total number of ways in which the bricks can be arranged on the wall so that a new configuration arises every time. He calls it M. Since Red John is back after a long time, he has also gained a masters degree in Mathematics from a reputed university. So, he wants Patrick to calculate the number of prime numbers (say P) up to M (i.e. <= M). If Patrick calculates P, Teresa should call Red John on the phone number from the safe and he will surrender if Patrick tells him the correct answer. Otherwise, Teresa will get another murder call after a week.

You are required to help Patrick correctly solve the puzzle.



The first line of input will contain an integer T followed by T lines each containing an integer N. 1<=T<=20, 1<=N<=40



Print exactly one line of output for each test case. The output should contain the number P.

Sample test(s)






For N = 1, the brick can be laid in 1 format only

The number of primes <= 1 is 0 and hence the answer.

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

There are 5 ways of arranging the bricks for N = 7 and there are 3 primes <= 5 and hence the answer 3.


Source : Hackerrank.com

Contest arranged by প্রোগ্রামিং প্রবলেম (Programming Problem in Bengali)

hide comments
aryan29: 2020-04-01 23:44:20

M val for N=40 is 217286

shubham97361: 2019-06-09 18:16:49

Similar to tiling problem.

aman_sachin200: 2018-06-13 22:54:52

Once you get the recurrence then it's a Cakewalk!!!!AC in 1 go!!!:P

hodobox: 2016-07-10 14:29:20

max(M) < 3*10^5

geoffreymace7: 2016-06-18 00:43:55

M can reach values upto 10^6....

★ QUEEN LENOCKA ★ : 2015-01-28 15:03:56

@Md. Najmuzzaman can you give more test cases, pretty please? for example for 40... still getting WA

Shashank: 2014-12-30 10:52:20

@Md. Najmuzzaman plz check my code ....it is ri8 for all test cases but still....WA

Added by:Najmuzzaman
Time limit:0.200s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64