Bnu 34985 Elegant String (matrix Rapid power + dp derivation formula)
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
# Include
# Include
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
> = 1; B = multiple (B, B, n, p);} return t;} void init (ll k) {B. set (); for (ll I = 0; I