SPOJ:Red John is Back(DP)
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)的更多相关文章
- spoj 1812 LCS2(SAM+DP)
[题目链接] http://www.spoj.com/problems/LCS2/en/ [题意] 求若干个串的最长公共子串. [思路] SAM+DP 先拿个串建个SAM,然后用后面的串匹配,每次将所 ...
- SPOJ BALNUM - Balanced Numbers - [数位DP][状态压缩]
题目链接:http://www.spoj.com/problems/BALNUM/en/ Time limit: 0.123s Source limit: 50000B Memory limit: 1 ...
- HZAU 1199 Little Red Riding Hood(DP)
Little Red Riding Hood Time Limit: 1 Sec Memory Limit: 1280 MBSubmit: 853 Solved: 129[Submit][Stat ...
- 【BZOJ1419】 Red is good [期望DP]
Red is good Time Limit: 10 Sec Memory Limit: 64 MB[Submit][Status][Discuss] Description 桌面上有R张红牌和B张 ...
- 计蒜客 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 ...
- 【BZOJ 1419】Red is good [概率DP]
我 是 Z Z 概率好玄啊(好吧是我太弱.jpg Description 桌面上有R张红牌和B张黑牌,随机打乱顺序后放在桌面上,开始一张一张地翻牌,翻到红牌得到1美元,黑牌则付出1美元.可以随时停止翻 ...
- SPOJ 1435 Vertex Cover 树形DP
i 表示节点 i ,j=0表示不选择其父节点,j=1表示选择其父节点.f 为其父节点. 取 每个节点选择/不选择 两者中较小的那个. 一组数据: 151 21 31 41 1010 910 1112 ...
- spoj 10606 Balanced Numbers 数位dp
题目链接 一个数称为平衡数, 满足他各个数位里面的数, 奇数出现偶数次, 偶数出现奇数次, 求一个范围内的平衡数个数. 用三进制压缩, 一个数没有出现用0表示, 出现奇数次用1表示, 出现偶数次用2表 ...
- SPOJ.TLE - Time Limit Exceeded(DP 高维前缀和)
题目链接 \(Description\) 给定长为\(n\)的数组\(c_i\)和\(m\),求长为\(n\)的序列\(a_i\)个数,满足:\(c_i\not\mid a_i,\quad a_i\& ...
随机推荐
- mysql delete auto_increment列时的注意问题
1. 说明 在对带有AUTO_INCREMENT列的表delete掉所有数据时: (1)对于MyISAM表,在delete表中所有数据时没有任何风险,随意折腾: (2)对于InnoDB表,在delet ...
- 用ELK 实时处理搜索日志
转载请标明原处:http://blog.csdn.net/hu948162999/article/details/50563110 本来这块业务 是放到SolrCloud上去的 , 然后 採用solr ...
- Odoo10尝鲜: 采购协议
Odoo10 对 call for bid 进行了 改进, 作为 '采购协议' 进入 采购, 选择 'Purchase agreement' 在 agreement 列表 建立新的 协议 选择 协议类 ...
- mysql 控制台环境下查询中文数据乱码,插入、更新中文数据不成功
mysql 控制台环境下查询中文数据乱码,插入.更新中文数据不成功 登录mysql密码是加入编码参数--default-character-set,中文用gbk mysql -uroo ...
- C#中??和?分别是什么意思? 在ASP.NET开发中一些单词的标准缩写 C#SESSION丢失问题的解决办法 在C#中INTERFACE与ABSTRACT CLASS的区别 SQL命令语句小技巧 JQUERY判断CHECKBOX是否选中三种方法 JS中!=、==、!==、===的用法和区别 在对象比较中,对象相等和对象一致分别指的是什么?
C#中??和?分别是什么意思? 在C#中??和?分别是什么意思? 1. 可空类型修饰符(?):引用类型可以使用空引用表示一个不存在的值,而值类型通常不能表示为空.例如:string str=null; ...
- Android摄像头採集的视频数据流怎样通过Socket实时发送到目标服务端
分两块: 1.取得摄像头採集的视频流 2.发送到server端 protected MediaRecorder mMediaRecorder; private LocalServerSocket mL ...
- web前端面试系列 - 数据结构(两个栈模拟一个队列)
一. 用两个栈模拟一个队列 思路一: 1. 一个栈s1作为数据存储,另一个栈s2,作为临时数据存储. 2. 入队时将数据压人s1 3. 出队时将s1弹出,并压人s2,然后弹出s2中的顶部数据,最后再将 ...
- Android录屏命令、Android录Gif、Android录视频
NoHttp开源地址:https://github.com/yanzhenjie/NoHttp NoHttp具体使用文档已公布,你想知道的全都有,请点我移步! 版权声明:转载请注明本文转自严振杰的博客 ...
- kubernetes集群管理命令(三)
系列目录 前面两节我们由浅入深介绍了不少kubernetes管理比较常用的命令.本节我们通过案例讲解一些需要更为复杂的操作才能完成的命令. 选择一个deployment下的所有pod 前面讲到过,ku ...
- input 的read only 和 disable的区别
read only ---------->只能读,不能操作,但是数据可以提交 disable -------------->控件被禁用,数据不能提交