Question:
For multiple groups of data, each group calculates the N number containing '123' (cannot be disconnected), for example, 1: 666, 2: 1666,14: 666.
Question:
I didn't think about the AC automatic solution, but I didn't know the digital DP. Here there is a digital tree similar to the digital DP, but it is different from the digital DP.
Digital tree:
Splits the number N into multiple segments like a line segment tree and performs recursive processing to obtain the answer.
Details(LUE)Solution:
Let's look at the number of each digit, And then subtract n from the corresponding number to convert the next layer to a subproblem: "When there are B consecutive 6 at the beginning, calculate the number of A with '123 ". It's so simple, so simple !!!!
Code is coming!
# Include <cstdio> # include <cstring> # include <algorithm> # define N 20/* Maximum number of guaranteed digits */using namespace STD; long N; long long F0 [N], F1 [N], F2 [N], F3 [N]; long F10 [N], F5 [N], f6 [4] [N];/* Note 1: [N] is the number of digits. Note 2: The number mentioned here is not the first 0, that is, 0066 is the four-digit F0: f1: Number of numbers with the first digit not 6 and without '123' (the second digit is not '6') F2: number of numbers with the first value of 6 and without '123' (the second digit is '6') F3: Number of numbers with '123' F5: 5. Number of numbers starting with '200' F6: 1: Number of numbers starting with '6' 2: Number of numbers starting with '66' 3: number of numbers containing '200' F10: Same as F3 */long power (long l Ong X, long p) {long ans = 1; while (P> 0) {If (P & 1) ans * = x; x * = X; p> = 1;} return ans;} void DFS (long NN, long nm)/* number of digits, number of consecutive 6 */{If (! Nn)/* No bit. End! */{Printf ("\ n"); return;} long I, j, k; If (NM> = 3) /* pruning (if the front part is '20140901', you can calculate it directly) */{long tens = 1; n --; for (I = 1; Tens <= N; I ++, TENs * = 10); For (; I <= nn; I ++) printf ("0"); If (n) printf ("% i64d ", n); printf ("\ n"); return;} If (F5 [NN]> = N)/* this bit is 0 ~ 5 */{printf ("% i64d", (n-1)/F10 [nn-1]); n = (n-1) % F10 [nn-1] + 1; DFS (nn-1, 0); return;} else if (F6 [3-nm] [NN]> = N)/* this bit is 6 */{printf ("6 "); n-= F5 [NN]; DFS (nn-1, Nm + 1); return;} else/* this bit is 7 ~ 9 */{n-= F6 [3-nm] [NN]; printf ("% i64d", (n-1)/F10 [nn-1] + 7); n = (n-1) % F10 [nn-1] + 1; DFS (nn-1, 0); Return ;}} void Init ()/* preprocessing */{long I; F0 [0] = 1; for (I = 1; I <n; I ++) {F0 [I] = (F0 [I-1] + F1 [I-1] + F2 [I-1]) * 9; f1 [I] = f0 [I-1]; F2 [I] = F1 [I-1]; F3 [I] = F3 [I-1] x 10 + F2 [I-1];} for (I = 3; I <n; I ++)/* number of digits containing '000000' */{F10 [I] = F3 [I]; f5 [I] = 6 * F10 [I-1]; F6 [3] [I] = F10 [I]-3 * F10 [I-1]; f6 [2] [I-1] = F6 [3] [I]-F5 [I]-3 * F10 [I-2]; f6 [1] [I-2] = F6 [2] [I-1]-F5 [I-1]-3 * F10 [I-3];} int main () {long I, g; scanf ("% i64d", & G); Init (); While (g --) {scanf ("% i64d", & N); for (I = 3; f10 [I] <n; I ++); DFS (I, 0);} return 0 ;}
[Poj3208] What is the most difficult digit DP for poj? (The positive solution is the AC automatic machine and the two-digit DP. I am surprised)