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. MySQL的1067错误

    1.打开my.ini文件,找到default-storage-engine=InnoDB这一行,把它改成default-storage-engine=MyISAM.*** my.ini必须为ansi格 ...

  2. asyncTask 的execute和executeOnExecutor 方法

    asyncTask.execute Android.os.Build.VERSION_CODES.DONUT, this was changed to a pool of threads allowi ...

  3. hdoj 4828 卡特兰数取模

    Grids Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Sub ...

  4. python(24)- 面向对象进阶

    面向对象基础知识: 1.面向对象是一种编程方式,此编程方式的实现是基于对类和对象的使用: 2.类是一个模板,模板中包装了多个‘函数’供使用(可以将多函数中公用的变量封装到对象中): 3.对象,根据模板 ...

  5. 【转载】聊一聊C#的Equals()和GetHashCode()方法

    首先先谈一下Equals()这个方法: Equals()方法,来自于Object,是我们经常需要重写的方法.此方法的默认实现大概是这样的: public virtual bool Equals(obj ...

  6. AsyncTask源代码解析

    快要毕业了.近期在阿里巴巴校园招聘面试,一面过了,感觉挺轻松,可能是运气好.面试官感觉比我腼腆一些.我俩从android绕到了spring mvc 到数据库悲观锁 到linux 然后又会到了andro ...

  7. response响应和User-Agent历史

    返回百度的源码,没有任何伪装: response是服务器响应的类文件,除了支持文件操作的方法以外,还支持以下方法:

  8. Linux下Tun/Tap设备通信原理

    Tun/Tap都是虚拟网卡,没有直接映射到物理网卡,是一种纯软件的实现.Tun是三层虚拟设备,能够处理三层即IP包,Tap是二层设备,能处理链路层网络包如以太网包.使用虚拟网络设备,可以实现隧道,如O ...

  9. 对A-Star寻路算法的粗略研究

    首先来看看完成后的效果: 其中灰色代表路障,绿色是起点和移动路径,红色代表终点   // = openArray[i+1].F) { minNode = openArray[i+1]; } } sta ...

  10. 在字符串资源文件里加入HTML元素,直接使用字符串资源,HTML元素没起作用的解决的方法

    escape  html  in string resource 一. 需求描写叙述 给TextView赋值res资源库中的字符串资源,注意这里是一个string资源,要实现以下的效果 "未 ...