URAL1586——DP——Threeprime Numbers

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

input output
4

204

大意:给你一个n表示几位数,让你输出满足三素数的数有多少个,三素数的定义为每三个连续的数都是一个素数

自己想到了大致要定义一个prime[j][k][l]来判断,还是想不出状态转移方程

状态转移方程  下一个状态可以由dp[i-1][k][l] 得到,所要添加的只是最高位,只要满足最高位以及接下来的两个数的集合也满足的话就行

也就是   dp[i][j][k] = dp[i][j][k] + dp[i-1][k][l],前一个状态的dp值是i-1位的时候所有满足条件的情况所以只要判prime[j][k][l]是素数就行

PS:

今天只A了一道0.0 看了原来的题目对于dp的for上下两个循环有了一点新的感觉

假如那道免费馅饼  dp[i][j] = max(max(dp[i-1][j],dp[i-1][j+1]),dp[i-1][j+1]);

那么要得到dp[i][j] 所得的都是dp[i-1]这个时间推来的,所以必须先把这种状态算出来

for(int i = 1; i <= t1; i++)

 for(int j = 0 ; j <= 10;j++)

把i-1这个状态的值都得出来

#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;

long long  dp[10100][10][10];

int prime[10][10][10];

const int mod = 1e9 + 9;

void solve()

{

    int j;

    memset(prime,0,sizeof(prime));

    for(int i = 100; i <= 999;i++){

        for( j = 2; j < i;j++){

            if(i%j == 0)

            break;

        }

        if(j >= i){

            int x1 = i/100;

            int x2 = (i%100)/10;

            int x3 = i%10;

            prime[x1][x2][x3] = 1;

        }

    }

      

    for(int i = 1; i <= 9; i++){

        for(int j = 0 ; j <= 9;j++){

            for(int k = 0 ; k <= 9; k++){

                if(prime[i][j][k])

                dp[3][i][j]++;

            }

        }

    }

    for(int i = 4; i <= 10000;i++){

        for(int j = 1; j <= 9; j++){

            for(int k = 0 ; k <= 9; k++){

                for(int l = 0 ; l <= 9;l++){

                    if(prime[j][k][l])

                    dp[i][j][k] = (dp[i-1][k][l] + dp[i][j][k]) % mod;



                }

            }

        }

    }

}

int main()

{

    solve();

    int n;

    while(~scanf("%d",&n)){

        long long  res = 0;

        for(int i = 1; i <= 9;i++){

            for(int j = 0 ; j <= 9;j++){

            res += 1ll*dp[n][i][j];

            res%=mod;

            }

        }

        printf("%lld\n",res);

    }

    return 0;

}

  

 

你可能感兴趣的:(number)