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.

Input

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

Output

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

Sample test(s)

input

2
1
7

output

0
3

Note

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)

题意:给定4*N的空格子,现在叫你用1*4或者4*1的板子去填放,问有多少种放法M,输出小于M的素数个数pM。

思路:如果是2*N用1*2或者2*1格子填的题,推出是斐波拉契数列。其特点是如果横着放,那么上下连续几块都要横着放。此题即是对于1*N的格子,用1*4的格子填有多少种方案。dp记录即可。

(今天啦啦操表演,所以emm,刷刷水题。

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
const int maxm=;
int dp[maxn][maxn],sum[maxn];
int p[maxm],vis[maxm+],num[maxm],tot;
void prime()
{
for(int i=;i<=maxm;i++){
if(!vis[i]) p[++tot]=i;
for(int j=;j<=tot&&i*p[j]<=maxm;j++){
vis[i*p[j]]=;
if(i%p[j]==) break;
}
num[i]=num[i-]+(-vis[i]);
}
}
int main()
{
int T,N,i,j,k;
prime();
for(i=;i<=;i++) dp[i][]=;
for(i=;i<=;i++){
for(j=i*;j<=;j++)
for(k=(i-)*;k<=j-;k++)
dp[j][i]+=dp[k][i-];
}
for(i=;i<=;i++){
for(j=;j<=i;j++)
for(k=;k<=j/;k++)
sum[i]+=dp[j][k];
sum[i]+=;
}
scanf("%d",&T);
while(T--){
scanf("%d",&N);
printf("%d\n",num[sum[N]]);
}
return ;
}

SPOJ:Red John is Back(DP)的更多相关文章

  1. spoj 1812 LCS2(SAM+DP)

    [题目链接] http://www.spoj.com/problems/LCS2/en/ [题意] 求若干个串的最长公共子串. [思路] SAM+DP 先拿个串建个SAM,然后用后面的串匹配,每次将所 ...

  2. SPOJ BALNUM - Balanced Numbers - [数位DP][状态压缩]

    题目链接:http://www.spoj.com/problems/BALNUM/en/ Time limit: 0.123s Source limit: 50000B Memory limit: 1 ...

  3. HZAU 1199 Little Red Riding Hood(DP)

    Little Red Riding Hood Time Limit: 1 Sec  Memory Limit: 1280 MBSubmit: 853  Solved: 129[Submit][Stat ...

  4. 【BZOJ1419】 Red is good [期望DP]

    Red is good Time Limit: 10 Sec  Memory Limit: 64 MB[Submit][Status][Discuss] Description 桌面上有R张红牌和B张 ...

  5. 计蒜客 Red Black Tree(树形DP)

    You are given a rooted tree with n nodes. The nodes are numbered 1..n. The root is node 1, and m of ...

  6. 【BZOJ 1419】Red is good [概率DP]

    我 是 Z Z 概率好玄啊(好吧是我太弱.jpg Description 桌面上有R张红牌和B张黑牌,随机打乱顺序后放在桌面上,开始一张一张地翻牌,翻到红牌得到1美元,黑牌则付出1美元.可以随时停止翻 ...

  7. SPOJ 1435 Vertex Cover 树形DP

    i 表示节点 i ,j=0表示不选择其父节点,j=1表示选择其父节点.f 为其父节点. 取 每个节点选择/不选择 两者中较小的那个. 一组数据: 151 21 31 41 1010 910 1112 ...

  8. spoj 10606 Balanced Numbers 数位dp

    题目链接 一个数称为平衡数, 满足他各个数位里面的数, 奇数出现偶数次, 偶数出现奇数次, 求一个范围内的平衡数个数. 用三进制压缩, 一个数没有出现用0表示, 出现奇数次用1表示, 出现偶数次用2表 ...

  9. SPOJ.TLE - Time Limit Exceeded(DP 高维前缀和)

    题目链接 \(Description\) 给定长为\(n\)的数组\(c_i\)和\(m\),求长为\(n\)的序列\(a_i\)个数,满足:\(c_i\not\mid a_i,\quad a_i\& ...

随机推荐

  1. MY JAVA-NOTE FIRST DAY

    今天是第一天开通博客,我很开心,总算拥有了自己的博客了,以后我会经常在博客里分享一些JAVA的心得.

  2. php 的$_POST 和$_REQUEST的区别

    发现$_POST 没数据,而$_REQUEST 有数据 ----------------------------------------- https://stackoverflow.com/ques ...

  3. scala使用FunSpec进行单元测试报错

    遇到比较奇怪的问题,mvn项目中的main函数可以正常运行,但是test阶段出错,报错信息如下: Exception in thread "main" java.lang.NoCl ...

  4. ListView中button监听器 设置 及 优化

    在应用开发中常常会用到ListView,而且每个Item里面都会有button之类的须要进行事件监听的控件.在给button加入OnClickListener的时候,一開始非常下意识的会想在ListV ...

  5. CrtmpServer 接收推送视频流 注册流基本流程

    今天研究了CrtmpServer 将客户端推动过来的视频流注册到服务的流程,记录下来,以备后用. 图1 注册前端视频流流程

  6. Logical Volume Manager (Linux)

    http://en.wikipedia.org/wiki/Logical_Volume_Manager_(Linux) Logical Volume Manager (Linux) From Wiki ...

  7. Linux kernel Wikipedia

    http://en.wikipedia.org/wiki/Linux_kernel Development model The current development model of the Lin ...

  8. alexNet--deep learning--alexNet的11行代码

    % Copyright 2016 The MathWorks, Inc. clear camera = webcam(  2  ); % Connect to the camerannet = ale ...

  9. Google Chrome的快捷键

    1.Ctrl + N 打开一个新窗口           &&            Alt + F4    关闭当前窗口 2.Ctrl + T 打开一个新的标签页 && ...

  10. 使用无缓冲IO函数读写文件

    前言 本文介绍使用无缓冲IO函数进行文件读写. 所谓的无缓冲是指该IO函数通过调用系统调用实现,其实系统调用内部的读写实现也是使用了缓冲技术的. 读写步骤 1. 打开文件 open 函数 2. 读写文 ...