• URAL 1586


    题目大意:求出n位十进制数中每相邻三位均为一个三位数素数的个数对10^9+9取模的值。

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

    数据规模:3<=n<=10000。

    理论基础:无。

    题目分析:1万位,不会是高精度吧?Come on,孩子,别忘了这道题的分类是dp。好了,言归正传。

    首先,我们用dp[i][j][k]表示i位十进制数中满足要求的且最高位为j,次高位为k的数的个数。bool数组pre[i][j][k]表示三位分别为ijk时是不是素数。

    然后,初始化,很显然,dp[3][j][k]=sum(pre[j][k][m],1<=m<=9)。

    下来,我们可以很轻易的得出状态转移方程:dp[i][j][k]=sum(pre[j][k][m]*dp[i-1][k][m],1<=m<=9)对10^9+9取模。

    最终的答案即为:ans=sum(dp[n][j][k],0<=k<=9,0<j<=9)对10^9+9取模。

    代码如下:

    #include<iostream>
    #include<cstring>
    #include<string>
    #include<cstdlib>
    #include<cstdio>
    #include<cmath>
    #include<algorithm>
    #include<queue>
    #include<ctime>
    #include<vector>
    using namespace std;
    typedef double db;
    #define DBG 0
    #define maa (1<<31)
    #define mii ((1<<31)-1)
    #define ast(b) if(DBG && !(b)) { printf("%d!!|
    ", __LINE__); while(1) getchar(); }  //调试
    #define dout DBG && cout << __LINE__ << ">>| "
    #define pr(x) #x"=" << (x) << " | "
    #define mk(x) DBG && cout << __LINE__ << "**| "#x << endl
    #define pra(arr, a, b)  if(DBG) {
        dout<<#arr"[] |" <<endl; 
        for(int i=a,i_b=b;i<=i_b;i++) cout<<"["<<i<<"]="<<arr[i]<<" |"<<((i-(a)+1)%8?" ":"
    "); 
        if((b-a+1)%8) puts("");
    }
    template<class T> inline bool updateMin(T& a, T b) { return a>b? a=b, true: false; }
    template<class T> inline bool updateMax(T& a, T b) { return a<b? a=b, true: false; }
    typedef long long LL;
    typedef long unsigned int LU;
    typedef long long unsigned int LLU;
    #define N 10000
    #define M 10
    int dp[N+1][M][M],n;
    bool pre[M][M][M]={
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,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0};
    int nprime[143]={101,
    103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,
    211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,
    331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,
    449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,
    587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,
    709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,
    853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,
    991,997};
    int main()
    {
        while(~scanf("%d",&n))
        {
            memset(dp,0,sizeof dp);
            for(int i=0;i<=142;i++)
            {
                dp[3][nprime[i]/100][nprime[i]/10%10]++;
            }
            for(int i=4;i<=n;i++)
            {
                for(int j=1;j<=9;j++)
                {
                    for(int k=0;k<=9;k++)
                    {
                        for(int l=1;l<=9;l+=2)
                        {
                            if(pre[j][k][l])dp[i][j][k]=(dp[i-1][k][l]+dp[i][j][k])%1000000009;
                        }
                    }
                }
            }
            int ans=0;
            for(int j=1;j<=9;j++)
            {
                for(int k=0;k<=9;k++)
                {
                    ans=(ans+dp[n][j][k])%1000000009;
                }
            }
            printf("%d
    ",ans);
        }
    	return 0;
    }
    

    其中,pre[i][j][k]的初始化可以依靠nprime[]数组得到。

    by:Jsun_moon http://blog.csdn.net/jsun_moon


  • 相关阅读:
    如何在Nginx下配置PHP程序环境
    PHPStorm2016如何安装主题
    Yii2初级入门教程
    递归递归!!
    PHP值传递和引用传递的区别
    网站移植到linux上后常犯的错误
    PHP之function_handling 函数
    PHP如何使用免费在线客服插件
    PHP快速入门 常用配置
    PHP快速入门 如何操作MySQL
  • 原文地址:https://www.cnblogs.com/bbsno1/p/3278149.html
Copyright © 2020-2023  润新知