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 ...
随机推荐
- Linux编译内核提示'make menuconfig' requires the ncurses libraries错误
原来使用的ubuntu 11.10系统由于误操作,导致系统崩溃,重新安装了ubuntu 11.10: 在编译内核的时候,提示如下错误: dingq@wd-u1110:~/hwsvn/2sw/1prj_ ...
- hdu 2645 find the nearest station
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2645 find the nearest station Description Since dande ...
- android开发系列之MVP设计模式
最近在开发一个android的项目中,发现了一个很实用的设计模式(MVP).大家可能一看到这个名字就有点蒙,MVP到底是什么鬼呢?它的好用到底体现在哪呢?别着急,下面就让我们一一分享出来. 说到MVP ...
- Bing Speech Recognition 标记
Bing Speech Services Bing Bing Speech Services provide speech capabilities for Windows and Windows ...
- 利用LibreOffice转换ppt、doc转化pdf
利用LibreOffice转换ppt.doc转化pdf LibreOffice下载地址: http://www.libreoffice.org/download/libreoffice-fresh/ ...
- 003--VS2013 C++ 多边形绘制
//全局变量HPEN hPen;HBRUSH hBru;POINT poly1[6], poly2[5], poly3[5]; //---------------------------------- ...
- python pandas/numpy
import pandas as pdpd.merge(dataframe1,dataframe2,on='common_field',how='outer') replace NaN datafra ...
- Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- Ant学习---第二节:Ant添加文件夹和文件夹集的使用
一.创建 java 项目(Eclipse 中),结构图如下: 1.创建 .java 文件,代码如下: package com.learn.ant; public class HelloWorld { ...
- javascript各种模式解析
1.工厂模式: 工厂模式是软件工程领域一种广为人知的设计模式,这种模式抽象了创建具体对象的过程(后面还将讨论其他设计模式及其在JavaScript 中的实现).考虑到在ECMAScript 中无法创建 ...