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. 定时任务crontab如何实现每秒执行?

    linux crontab 命令,最小的执行时间是一分钟.如需要在小于一分钟内重复执行,可以有两个方法实现. 方法一:crontab -l内容如下,则每10秒执行一次/home/fdipzone/ph ...

  2. .NET组件编程

    链接 http://www.cnblogs.com/mapserver/archive/2006/03/06/343632.html

  3. C中的继承和多态

    昨天同学面试被问到这个问题,很有水平,以前都没有遇到过这个问题,一时自己也不知道怎么回答. 网上学习了一下,记录以备后用! C/C++ Internals : 里面的问题都写的不错,可以读读! Ref ...

  4. C# 操作摄像头

    如有雷同,不胜荣幸,若转载,请注明 这个是大众普通方法,鉴于有网友和朋友问相同的问题.在这里将我的拙劣的代码关键部分贴出来.以便帮助很多其它的朋友们,不足之处甚多,我能够学习,交流,请教阁下 废话到此 ...

  5. Odoo(OpenERP)开发实践:通过XML-RPC接口訪问Odoo数据库

    Odoo(OpenERP)server支持通过XML-RPC接口訪问.操作数据库,基于此可实现与其它系统的交互与集成. 本文是使用Java通过XMLRPC接口操作Odoo数据库的简单演示样例.本例引用 ...

  6. 百度地图 创建 自定义控件(vue)

    1.组件代码 Bmap.vue <!-- 离线地图 组件 --> <template> <div id="map" :style="styl ...

  7. vue2.0 + vux (三)MySettings 页

    1.MySettings.vue <!-- 我的设置 --> <template> <div> <img class="img_1" sr ...

  8. Odoo超售订单

    当 交付给客户的货物多于订购的数量时,就形成'超售'状态: 对于超售的部分,需要进行开票处理,以及根据情况修改交货     发生超售的前提是,产品开票策略为 '按订购数量开票'     同时需要 允许 ...

  9. 笔记09 WS,WCF

    http://blog.csdn.net/avi9111/article/details/5655563 http://www.cnblogs.com/tearer/archive/2013/04/2 ...

  10. CCNET自动构建之路

    人永远追求效率(想偷懒),不想手动编译项目.发布站点于是产生了自动构建技术,.NET领域中CCNET是个不错的选择. 一路问题不少,记录一下. 准备环境 服务器上需要有iis.vs(与开发环境的版本一 ...