URAL 1586 Threeprime Numbers(DP)
题意 : 定义Threeprime为它的任意连续3位上的数字,都构成一个3位的质数。 求对于一个n位数,存在多少个Threeprime数。
思路 : 记录[100, 999]范围内所有素数(标记的是该素数的每一位x1,x2,x3)。然后从n = 4往后,定义dp[i][x2][x3], i 表示到第 i 位时,第 i-1 位为 x2 , 第 i 位x3,此时所包含的情况数。
dp[i][x2][x3] = dp[i][x2][x3] + dp[i-1][x1][x2];最后求和sum(dp[n][x2][x3]);
//
#include <cstdio>
#include <cstring>
#include <iostream>
#define MOD 1000000009
#define LL long long
using namespace std ; int dp[][][],prime[][][] ;
int vis[] = {} ,cnt = ; void solve()
{
cnt = ;
memset(vis,,sizeof(vis)) ;
for(int i = ; i <= ; i++)
{
if(!vis[i])
{
for(int j = i*i ; j <= ; j += i)
vis[j] = ;
}
}
for(int i = ; i <= ; i ++)
{
if(!vis[i])
{
int x1 = i / ;
int x2 = i / % ;
int x3 = i % ;
prime[x1][x2][x3] = ;
dp[][x2][x3] += ;
cnt ++ ;
}
}
}
int main()
{
int n ;
scanf("%d",&n);
solve() ;
if(n == ) {
printf("%d\n",cnt % MOD) ;
return ;
}
for(int i = ; i <= n ; i++)
for(int x1 = ; x1 <= ; x1 ++)
for(int x2 = ; x2 <= ; x2 ++)
for(int x3 = ; x3 <= ; x3 ++)
if(dp[i-][x1][x2] && prime[x1][x2][x3])
dp[i][x2][x3] = (dp[i][x2][x3] + dp[i-][x1][x2]) % MOD;
int ans = ;
for(int x2 = ; x2 <= ; x2 ++)
{
for(int x3 = ; x3 <= ; x3 ++)
{
ans = (ans+dp[n][x2][x3])%MOD ;
}
}
printf("%d\n",ans) ;
return ;
}
URAL 1586 Threeprime Numbers(DP)的更多相关文章
- 递推DP URAL 1586 Threeprime Numbers
题目传送门 /* 题意:n位数字,任意连续的三位数字组成的数字是素数,这样的n位数有多少个 最优子结构:考虑3位数的数字,可以枚举出来,第4位是和第3位,第2位组成的数字判断是否是素数 所以,dp[i ...
- URAL 1009 K-based numbers(DP递推)
点我看题目 题意 : K进制的N位数,不能有前导零,这N位数不能有连续的两个0在里边,问满足上述条件的数有多少个. 思路 : ch[i]代表着K进制的 i 位数,不含两个连续的0的个数. 当第 i 位 ...
- Gym 100703G---Game of numbers(DP)
题目链接 http://vjudge.net/contest/132391#problem/G Description standard input/outputStatements — It' s ...
- URAL 1146 Maximum Sum(DP)
Given a 2-dimensional array of positive and negative integers, find the sub-rectangle with the large ...
- POJ1338Ugly Numbers(DP)
http://poj.org/problem?id=1338 第一反应就是DP,DP[i] = min{2*DP[j], 3*DP[k], 5*DP[p] j,k,p<i};于是枚举一下0-i- ...
- Ural 1073 Square Country (DP)
题目地址:Ural 1073 DP水题.也能够说是背包. #include <iostream> #include <cstdio> #include <string&g ...
- Codeforces 403D: Beautiful Pairs of Numbers(DP)
题意:转换模型之后,就是1~n个数中选k个,放到一个容量为n的背包中,这个背包还特别神奇,相同的物品摆放的位置不同时,算不同的放法(想象背包空间就是一个长度为n的数组,然后容量为1的物体放一个格子,容 ...
- Ural 2018The Debut Album(DP)
题目地址:Ural 2018 简单DP.用滚动数组. 代码例如以下: #include <iostream> #include <cstdio> #include <st ...
- URAL 2031. Overturned Numbers (枚举)
2031. Overturned Numbers Time limit: 1.0 second Memory limit: 64 MB Little Pierre was surfing the In ...
随机推荐
- virtualbox下 ubuntu 14.04设置外网独立IP
安装时记得选择sshserver vim /etc/network/interfaces iface eth0 inet static address YOUR IP netmask 子网掩码 get ...
- mongoDB 3.0 安全权限访问控制
MongoDB3.0权限,啥都不说了,谷歌百度出来的全是错的.先安装好盲沟,简单的没法说. 首先,不使用 —auth 参数,启动 mongoDB: mongodb-linux-i686-3.0.0/b ...
- OC中类的扩展介绍
对OC类的扩展总结如下: 共有4个: 1.子类 subClass 作用:可以使用类的继承来增添父类的变量和方法. 写法:在.h文件中 @interface Student : Person 2.分类 ...
- 002--VS C++ 获取鼠标坐标并显示在窗口上
//--------------------------------------------MyPaint() 函数------------------------------------------ ...
- C# SQL增删查改
DBHelper: /// <summary> /// 执行查询 /// </summary> /// <param name="sql">有效 ...
- PB小技巧集锦
1. 数据窗口检查重复行dw_1.SetSort ("user_id A")dw_1.Sort()dw_1.SetFilter ("user_id = user_id[- ...
- Class.forName("com.mysql.jdbc.Driver");的作用
对于大的项目当然我们都已经有了原有基本框架,但是对于一些新的技术探讨的时候,我们还是直接调用Class.forName("com.mysql.jdbc.Driver")连接数据库进 ...
- android 通过工具抓包
工具: 1.tcpdump :http://www.strazzere.com/android/tcpdump 2.wireshark:http://pan.baidu.com/s/1geooiav ...
- 不同平台下Java环境变量的设置
http://www.java.com/en/download/help/path.xml
- JavaWeb实现文件上传下载功能实例解析
转:http://www.cnblogs.com/xdp-gacl/p/4200090.html JavaWeb实现文件上传下载功能实例解析 在Web应用系统开发中,文件上传和下载功能是非常常用的功能 ...