递推DP URAL 1586 Threeprime Numbers
/*
题意:n位数字,任意连续的三位数字组成的数字是素数,这样的n位数有多少个
最优子结构:考虑3位数的数字,可以枚举出来,第4位是和第3位,第2位组成的数字判断是否是素数
所以,dp[i][j][k] 表示i位数字,最高位数字j,第二高位数字k
状态转移方程:dp[i][j][k] += dp[i-1][k][l]
注意:最高位从1开始枚举:)
详细解释:http://blog.csdn.net/zhangyanxing666/article/details/9628563
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std; const int MAXN = 1e4 + ;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + ;
int prime[][][];
int vis[];
int dp[MAXN][][]; void solve(void)
{
memset (prime, , sizeof (prime));
memset (vis, , sizeof (vis));
memset (dp, , sizeof (dp));
for (int i=; i<=; ++i)
{
if (!vis[i])
{
vis[i] = true;
for (int j=i*; j<=; j+=i)
{
vis[j] = true;
}
prime[i/][i/%][i%] = ;
}
} for (int i=; i<=; ++i)
{
for (int j=; j<=; ++j)
{
for (int k=; k<=; ++k) if (prime[i][j][k]) dp[][i][j]++;
}
} for (int i=; i<=; ++i)
{
for (int j=; j<=; ++j)
{
for (int k=; k<=; ++k)
{
for (int l=; l<=; ++l)
if (prime[j][k][l]) dp[i][j][k] = (dp[i][j][k] + dp[i-][k][l]) % MOD;
}
}
}
} int main(void) //URAL 1586 Threeprime Numbers
{
//freopen ("M.in", "r", stdin); solve ();
int n;
while (scanf ("%d", &n) == )
{
int ans = ;
for (int i=; i<=; ++i)
{
for (int j=; j<=; ++j)
ans = (ans + dp[n][i][j]) % MOD;
} printf ("%d\n", ans);
} return ;
}
递推DP URAL 1586 Threeprime Numbers的更多相关文章
- 递推DP URAL 1009 K-based Numbers
题目传送门 题意:n位数,k进制,求个数分析:dp[i][j] 表示i位数,当前数字为j的个数:若j==0,不加dp[i-1][0]; 代码1: #include <cstdio> #in ...
- 递推DP URAL 1017 Staircases
题目传送门 /* 题意:给n块砖头,问能组成多少个楼梯,楼梯至少两层,且每层至少一块砖头,层与层之间数目不能相等! 递推DP:dp[i][j] 表示总共i块砖头,最后一列的砖头数是j块的方案数 状态转 ...
- 递推DP URAL 1260 Nudnik Photographer
题目传送门 /* 递推DP: dp[i] 表示放i的方案数,最后累加前n-2的数字的方案数 */ #include <cstdio> #include <algorithm> ...
- 递推DP URAL 1353 Milliard Vasya's Function
题目传送门 /* 题意:1~1e9的数字里,各个位数数字相加和为s的个数 递推DP:dp[i][j] 表示i位数字,当前数字和为j的个数 状态转移方程:dp[i][j] += dp[i-1][j-k] ...
- 递推DP URAL 1119 Metro
题目传送门 /* 题意:已知起点(1,1),终点(n,m):从一个点水平或垂直走到相邻的点距离+1,还有k个抄近道的对角线+sqrt (2.0): 递推DP:仿照JayYe,处理的很巧妙,学习:) 好 ...
- 递推DP URAL 1031 Railway Tickets
题目传送门 /* 简单递推DP:读题烦!在区间内的都更新一遍,dp[]初始化INF 注意:s1与s2大小不一定,坑! 详细解释:http://blog.csdn.net/kk303/article/d ...
- 递推DP URAL 1167 Bicolored Horses
题目传送门 题意:k个马棚,n条马,黑马1, 白马0,每个马棚unhappy指数:黑马数*白马数,问最小的unhappy值是多少分析:dp[i][j] 表示第i个马棚放j只马的最小unhappy值,状 ...
- URAL 1586 Threeprime Numbers(DP)
题目链接 题意 : 定义Threeprime为它的任意连续3位上的数字,都构成一个3位的质数. 求对于一个n位数,存在多少个Threeprime数. 思路 : 记录[100, 999]范围内所有素数( ...
- 递推DP URAL 1081 Binary Lexicographic Sequence
题目传送门 题意:问第k个长度为n的01串是什么(不能有相邻的1) 分析:dp[i][0/1] 表示前i个,当前第i个放1或0的方案数,先预处理计算,dp[i][1]只能有dp[i-1][0]转移过来 ...
随机推荐
- shell编程报错 [: missing `]'
NGINX_RATES=50 NGINX_BURST=3000 NGINX_PATH=/opt/srv/nginx/conf/nginx.conf BEE_PATH=/opt/srv/nginx/co ...
- android edittext 去边框
EditText的background属性设置为@null就搞定了:android:background="@null" style属性倒是可加可不加 附原文:@SlumberMa ...
- 15 条实用 Linux/Unix 磁带管理命令
导读 磁带设备应只用于定期的文件归档或将数据从一台服务器传送至另一台.通常磁带设备与 Unix 机器连接,用 mt 或 mtx 控制.强烈建议您将所有的数据同时备份到磁盘(也许是云中)和磁带设备中. ...
- 在iOS7中修改状态栏字体的颜色
http://www.2cto.com/kf/201408/324442.html 默认状态栏的字体为黑色:UIStatusBarStyleDefault 状态栏的字体为白色:UIStatusBarS ...
- Linux查看和结束进程命令详解
在ubuntu中,终止一个进程或终止一个正在运行的程序,一般是通过 kill .killall.pkill.xkill 等进行. ----------------------------------- ...
- Two Sum I & II
Two Sum I Given an array of integers, find two numbers such that they add up to a specific target nu ...
- 特殊表达式的意义[c++ special expressions]
[本文链接] http://www.cnblogs.com/hellogiser/p/special-expressions.html x&(x-1)表达式的意义: 统计二进制中1的个数. ...
- android 初探
2014年7月27日 15:02:57 附: android 官方培训课程中文版 //官方简单的入门教程, 每个大类中只介绍了几个知识点, 可以快速搭建一个hello world android 开发 ...
- Java for LeetCode 066 Plus One
Given a non-negative number represented as an array of digits, plus one to the number. The digits ar ...
- August 5th, 2016, Week 32nd, Friday
Life is made up of small pleasures. 生活由各种细小的幸福构成. Don't expect too much. I am not qualified to get m ...