Recursive DP URAL 1586 threeprime Numbers

Source: Internet
Author: User

Topic Portal

1 /*2 Test instructions: n digits, any consecutive three-digit number is a prime number, so how many n digits3 Optimal substructure: Consider a 3-digit number, which can be enumerated, the 4th and 3rd digits, and the 2nd digit determines whether the number is prime4 So, Dp[i][j][k] represents the I-digit, the highest digit J, the second high digit K5 State transfer equation: dp[i][j][k] + = Dp[i-1][k][l]6 Note: The highest bit is enumerated starting from 1:)7 Detailed Explanation:http://blog.csdn.net/zhangyanxing666/article/details/96285638 */9#include <cstdio>Ten#include <iostream> One#include <algorithm> A#include <cstring> -#include <cmath> - using namespacestd; the  - Const intMAXN = 1e4 +Ten; - Const intINF =0x3f3f3f3f; - Const intMOD = 1e9 +9; + intprime[ One][ One][ One]; - intvis[1010]; + intdp[maxn][ One][ One]; A  at voidSolvevoid) - { -memset (Prime,0,sizeof(prime)); -memset (Vis,0,sizeof(Vis)); -Memset (DP,0,sizeof(DP)); -      for(intI=2; i<= +; ++i) in     { -         if(!Vis[i]) to         { +Vis[i] =true; -              for(intj=i*2; j<= +; j+=i) the             { *VIS[J] =true; $             }Panax Notoginsengprime[i/ -][i/Ten%Ten][i%Ten] =1; -         } the     } +  A      for(intI=1; i<=9; ++i) the     { +          for(intj=0; j<=9; ++j) -         { $              for(intk=0; k<=9; ++K)if(Prime[i][j][k]) dp[3][i][j]++; $         }  -     } -  the      for(intI=4; i<=10000; ++i) -     {Wuyi          for(intj=1; j<=9; ++j) the         { -              for(intk=0; k<=9; ++k) Wu             { -                  for(intL=0; l<=9; ++l) About                     if(Prime[j][k][l]) dp[i][j][k] = (Dp[i][j][k] + dp[i-1][K][L])%MOD; $             } -         } -     } - } A  + intMainvoid)//URAL 1586 Threeprime Numbers the { -     //freopen ("m.in", "R", stdin); $  the solve (); the     intN; the      while(SCANF ("%d", &n) = =1) the     { -         intAns =0; in          for(intI=1; i<=9; ++i) the         { the              for(intj=0; j<=9; ++j) AboutAns = (ans + dp[n][i][j])%MOD; the         } the  theprintf ("%d\n", ans); +     } -  the     return 0;Bayi}

Recursive DP URAL 1586 threeprime Numbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.