DP(11)位数dp

M - Threeprime Numbers

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

Rest at the sea is wonderful! However, programmer Pasha became awfully bored of lying on a beach in Turkey; so bored that he decided to count the quantity of three-digit prime numbers. This turned out to be so interesting that he then started to study threeprime numbers. Pasha calls an integer a threeprime number if any three consecutive digits of this integer form a three-digit prime number. Pasha had already started working on the theory of the divine origin of such numbers when some vandals poured water on Pasha and cried some incomprehensible words like “Sonnenstich!”, “Colpo di sole!”, and “Coup de soleil!”
You are to continue Pasha’s work and find out how often (or rare) threeprime numbers are.

Input

The input contains an integer n (3 ≤ n ≤ 10000).

Output

Output the quantity of n-digit threeprime numbers calculated modulo 10 9 + 9.

Sample Input

inputoutput
4
204
 
 
#include<cstdio>
#include<algorithm>
#include<cmath>
const int MOD = 1e9+9;
int prime[15][15][15];
int dp[10005][15][15];
using namespace std;
int main(){
    int n;
    scanf("%d",&n);
    prime[0][0][2] = 1;
    for(int i = 3; i < 1000; i++){
        bool flag = 1;
        for(int j = 2; j <= sqrt(i); j++)
            if(i%j==0){
                flag = 0;break;
            }
            prime[i/100][i/10%10][i%10] = flag;
    }
    for(int i = 1; i < 10; i++)
        for(int j = 0; j < 10; j++)
            for(int k = 0; k < 10; k++){
                if(prime[i][j][k])
                    dp[3][i][j]++;
            }
    for(int i = 4; i <= n; i++)
        for(int j = 1; j < 10; j++)
            for(int k = 0; k < 10; k++)
                for(int h = 0; h < 10; h++){
                    if(prime[j][k][h])
                    dp[i][j][k] += dp[i-1][k][h],
                    dp[i][j][k] %= MOD;
    }
    int ans = 0;
    for(int i = 1; i < 10; i++)
        for(int j = 0; j < 10; j++)
            ans += dp[n][i][j],ans %= MOD;
    printf("%d\n",ans);
    return 0;
}

 

posted @ 2015-09-06 23:13  Tobu  阅读(197)  评论(0)    收藏  举报