Elegant stringtime limit: 1000 msmemory limit: 65536kb64-bit integer Io format: % LLD Java class name: mainprev submit status Statistics discuss nextwe define a kind of strings as elegant string: among all the substrings of an elegant string, none of them is a permutation of "0, 1 ,..., K ". let function (n, k) be the number of elegant strings of length n which only contains digits from 0 to K (random SIVE ). please calculate function (n, k ). inputinput starts with an integer T (T ≤ 400), denoting the number of test cases. each case contains two integers, N and K. N (1 ≤ n ≤ 1018) represents the length of the strings, and K (1 ≤ k ≤ 9) represents the biggest digit in the string. outputfor each case, first output the case number" Case # X:", And X is the case number. Then output function (n, k) mod 20140518 in this case. sample input
21 17 6
Sample output
Case #1: 2Case #2: 818503
Source2014 ACM-ICPC Beijing Invitational Programming Contest
Question and code:
# Include <iostream> # include <cstdio> # include <cstring> using namespace STD; typedef long ll; const long mod = 20140518; struct mat {ll T [10] [10]; void set () {memset (T, 0, sizeof (t) ;}} A, B; mat multiple (MAT a, mat B, ll N, ll p) {ll I, j, k; MAT temp; temp. set (); for (I = 0; I <n; I ++) for (j = 0; j <n; j ++) {if (. T [I] [J]! = 0) for (k = 0; k <n; k ++) temp. T [I] [k] = (temp. T [I] [k] +. T [I] [J] * B. T [J] [k]) % P;} return temp;} mat quick_mod (mat B, ll N, ll M, ll p) {mat t; T. set (); For (ll I = 0; I <n; I ++) T. T [I] [I] = 1; while (m) {If (M & 1) {T = multiple (T, B, n, p );} m> = 1; B = multiple (B, B, n, p);} return t;} void Init (ll k) {B. set (); For (ll I = 0; I <K; I ++) B. T [I] [0] = 1; for (ll I = 1; I <K; I ++) {for (LL J = I-1; j <K; j ++) if (j = i-1) B. T [J] [I] = k-I + 1; els E B. T [J] [I] = 1 ;}/ * For (INT I = 0; I <K; I ++) {for (Int J = 0; j <K; j ++) printf ("% LLD", B. T [I] [J]); puts ("");} */} int main () {int CAS; ll N; ll K; scanf ("% d ", & CAS); For (int ca = 1; Ca <= CAS; CA ++) {scanf ("% LLD", & N, & K ); init (k); A = quick_mod (B, K, n-1, MoD); LL ans = 0; For (ll I = 0; I <K; I ++) {ans = (ANS + (k + 1) *. T [0] [I]) % MOD;} printf ("case # % d: % LLD \ n", CA, ANS);} return 0 ;} /* The Question of the Beijing Invitational competition, I was a senior A at the time. Since then, I have learned that there is a matrix power. This thing. Then I went back to school and learned it. I felt that it was the most important to solve the DP formula. Other templates would be OK. First, let's take a look at the meaning of this question: to define a string, any of its strings cannot have any full arrangement of 0-K, so we call this string elegant string. Next, we define a function f (n, k) as the character length of N, using 0-K letters to constitute the number of elegant strings. Because N is very large, it is easy to think of using a matrix to speed up the power. Then the formula is deduced. We define DP [I] [J] as the number of strings whose lengths are I and whose tails are different from those of J letters. So we can easily introduce the recursive formula of DP [I] [J]: DP [I] [J] = DP [I-1] [J-1] * (k-J + 2) + dp [I-1] [J] + ...... + Dp [I-1] [k]; what does that mean? First we know that DP [I] and DP [I-1] are only one letter, assuming we add a letter after DP [I-1, this letter can be the same as one of several different letters at the end of DP [I-1], it can also be different (for example: If DP [I-1] = ...... 0123, K = 7, this is to form DP [I], we can add 4, 5, 6, 7, or 0, 1, 2, 3 at the end) if it is 4, 5, 6, 7, then DP [I-1] [J-1] becomes DP [I] [J] (see definition ), if it is 0, 1, 2, 3, then DP [I-1] [J-1] will correspondingly become DP [I] [J], DP [I] [J-1], DP [I] [J-2], DP [I] [J-3], in order to form a DP [I] [J], it can be constructed using DP [I-1] [J-1] --- DP [I-1] [K. Then the rest is to construct the matrix, which is very simple. Please indicate the source for reprinting. Thank you. */