Http://acm.timus.ru/problem.aspx? Space = 1 & num = 1586
I did not understand the meaning of the question. I read others' translations. Threeprime number indicates that a three-digit number composed of any three consecutive numbers is a prime number. Note that it must be a three-digit number. Give N and ask how many n digits meet the condition.
First, filter out and mark the prime numbers of the three digits, and set DP [I] [J] [k] to represent the I bit, the last two digits are the numbers where J and K meet the conditions.
So DP [I] [J] [k] + = DP [I-1] [g] [J].
# Include <stdio. h> # include <iostream> # include <map> # include <set> # include <list> # include <stack> # include <vector> # include <math. h> # include <string. h> # include <queue> # include <string> # include <stdlib. h >#include <algorithm> # define ll _ int64 # define EPS 1e-12 # define PI ACOs (-1.0) # define pp pair <LL, ll> using namespace STD; const int INF = 0x3f3f3f; const int maxn = 1000; const int mod = 1000000009; int PR IME [maxn + 10]; int flag [maxn + 10]; int CNT = 0; int tprime [maxn + 10]; int DP [10010] [12] [12]; void get_prime () {memset (flag, 0, sizeof (FLAG); prime [0] = 0; flag [0] = Flag [1] = 1; for (INT I = 2; I <= maxn; I ++) {If (flag [I] = 0) {Prime [++ prime [0] = I; if (I> = 100 & I <= 999) tprime [++ CNT] = I;} For (Int J = 1; j <= prime [0] & I * prime [J] <= maxn; j ++) {flag [prime [J] * I] = 1; if (I % prime [J] = 0) Break ;}} int main () {Get _ Prime (); int N; while (~ Scanf ("% d", & N) {If (n = 3) {printf ("% d \ n", CNT); continue;} memset (DP, 0, sizeof (DP); For (INT I = 1; I <= CNT; I ++) {int B = tprime [I] % 10; int A = tprime [I]/10% 10; DP [3] [a] [B] + = 1;} For (INT I = 4; I <= N; I ++) {for (Int J = 0; j <= 9; j ++) {for (int K = 0; k <= 9; k ++) {for (int g = 0; G <= 9; G ++) {int num = J * 100 + K * 10 + G; if (flag [num] = 0 & amp; num & gt; = 100) // num must be a three-digit number {DP [I] [k] [g] + = DP [I-1] [J] [k] % MOD; DP [I] [k] [g] % = mod ;}}} ll ans = 0; For (INT I = 0; I <= 9; I ++) {for (Int J = 0; j <= 9; j ++) ans = (ANS + dp [N] [I] [J]) % MOD ;} printf ("% i64d \ n", ANS);} return 0 ;}
Ural threeprime numbers (DP)