题目链接

题意 : 定义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)的更多相关文章

  1. 递推DP URAL 1586 Threeprime Numbers

    题目传送门 /* 题意:n位数字,任意连续的三位数字组成的数字是素数,这样的n位数有多少个 最优子结构:考虑3位数的数字,可以枚举出来,第4位是和第3位,第2位组成的数字判断是否是素数 所以,dp[i ...

  2. URAL 1009 K-based numbers(DP递推)

    点我看题目 题意 : K进制的N位数,不能有前导零,这N位数不能有连续的两个0在里边,问满足上述条件的数有多少个. 思路 : ch[i]代表着K进制的 i 位数,不含两个连续的0的个数. 当第 i 位 ...

  3. Gym 100703G---Game of numbers(DP)

    题目链接 http://vjudge.net/contest/132391#problem/G Description standard input/outputStatements — It' s ...

  4. URAL 1146 Maximum Sum(DP)

    Given a 2-dimensional array of positive and negative integers, find the sub-rectangle with the large ...

  5. 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- ...

  6. Ural 1073 Square Country (DP)

    题目地址:Ural 1073 DP水题.也能够说是背包. #include <iostream> #include <cstdio> #include <string&g ...

  7. Codeforces 403D: Beautiful Pairs of Numbers(DP)

    题意:转换模型之后,就是1~n个数中选k个,放到一个容量为n的背包中,这个背包还特别神奇,相同的物品摆放的位置不同时,算不同的放法(想象背包空间就是一个长度为n的数组,然后容量为1的物体放一个格子,容 ...

  8. Ural 2018The Debut Album(DP)

    题目地址:Ural 2018 简单DP.用滚动数组. 代码例如以下: #include <iostream> #include <cstdio> #include <st ...

  9. URAL 2031. Overturned Numbers (枚举)

    2031. Overturned Numbers Time limit: 1.0 second Memory limit: 64 MB Little Pierre was surfing the In ...

随机推荐

  1. 手动书写小代码-foreach实现机制

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.C ...

  2. Effiective C++ (一)

    最近在看Effective  C++ ,同时将总结一下里边的重要知识点: ##########################    module  1    #################### ...

  3. TextSwitcher,译为文字转换器控件

    ViewSwitcher仅仅包含子类型TextView.TextSwitcher被用来使屏幕上的label产生动画效果.每当setText(CharSequence)被调用时,TextSwitcher ...

  4. mvvm 模式

    MVC = Massive View Controller ? 有笑话称MVC为重量级的试图控制器.仔细一想,确实存在这个问题.以UITableViewController和UITableView举个 ...

  5. -25299 reason: 'Couldn't add the Keychain Item.'

    今天在用苹果官方demo 提供的KeychainItemWrapper类时遇到-25299  reason: 'Couldn't add the Keychain Item.'错误,再4s上可以正常运 ...

  6. IOS- 网络图片缓存到沙盒中 ,离线取出。

    一.缓存图片 //1.首先创建在沙盒中创建一个文件夹用于保存图片 NSFileManager *fileManager = [[NSFileManager alloc] init]; NSString ...

  7. OO之工厂模式

    以下为工厂模式的详解,包括简单工厂,普通工厂模式,抽象工厂. 引子: 假设有一个交通工具公司,生产自行车,汽车,飞机等,现要销售该公司的产品,要怎么设计呢? 在交通工具商店中加一个if else判断如 ...

  8. 用C语言实现的扑克牌洗牌程序

    一副牌:54张 从0开始排序: 0-12表示黑桃   A 1,2,3,... 10,J,Q,K 13-25表示红桃 A 1,2,3,... 10,J,Q,K 26-38表示草花 A 1,2,3,... ...

  9. hadoop HA 之 QJM

    前言 本文主要通过对hadoop2.2.0集群配置的过程加以梳理,所有的步骤都是通过自己实际测试.文档的结构也是根据自己的实际情况而定,同时也会加入自己在实际过程遇到的问题.搭建环境过程不重要,重要点 ...

  10. Spring集成hibernate错误

    八月 25, 2016 7:55:31 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin警告: [SetPropertiesRule ...