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\& ...
随机推荐
- MinGW在Windows环境下配合命令提示符运行C/C++
http://jingyan.baidu.com/article/4853e1e5787d6b1909f726f8.html 在电脑中配置MinGW环境. 具体参见我的另一篇分享经验——MinGW在W ...
- 纯CSS3美化radio和checkbox
如题,主要通过CSS3来实现将radio和checkbox美化的效果.可是兼容性并非非常好,PC端仅仅支持chrome浏览器(IE和Firefox測试不行,其它没有很多其它測试).然后微信端和QQ端訪 ...
- gulp安装+一个超简单入门小demo
gulp安装參考.gulp安装參考2. 一.NPM npm是node.js的包管理工具.主要功能是管理.更新.搜索.公布node的包. Gulp是通过npm安装的. 所以首先,须要安装node.js. ...
- SharpSSH
SharpSSH sharpssh is a pure .NET implementation of the SSH2 client protocol suite. It provides an AP ...
- OpenGL中FBO的概念及其应用 [转]
http://www.cppblog.com/kongque/archive/2010/08/26/124754.html FBO一个最常见的应用就是:渲染到纹理(render to texture) ...
- 第四讲_图像识别之图像分类Image Classification
第四讲_图像识别之图像分类Image Classification 目录 图片分类 性能指标:top1,top5 ILSVRC:每种任务数据集不一样 imageNet:根据WorldNet组织的图片集 ...
- Hibernate调试——定位查询源头
本文是我在importNew翻译的文章,首发在importNew,这里会定期更新链接. 为什么有时Hibernate会在程序某一部分生成一条指定sql查询?这个问题让人非常难立马理解.当处理不是我们本 ...
- VM(转)
vmplayer && VMworkstation 很多人想尝试一下多种不同的操作系统,例如学习Linux:又或者希望搞一个专门的系统用来测试各种各样东西而不会搞乱搞坏现有的系统. ...
- vim 自动补全
1. vim编辑器自带关键字补全 触发: ctrl + n or ctrl + p 补全命令: <C-n> 普通关键字 [能够根据buffer以及标签文件列表等 ...
- ANDROID STUDIO 2.2.3 DOWNLOAD FROM DL.GOOGLE.COM
立即开始使用 Android Studio Android Studio 包含用于构建 Android 应用所需的所有工具. 下载 ANDROID STUDIO2.2.3 FOR WINDOWS (1 ...