Title Link: http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985
We define a kind of strings as elegant String:among all the substrings of a elegant string, none of them are a permut Ation of "0, 1,..., k".Let function (n, k) is the number of elegant strings of length n which only contains digits from 0 to K (inclusive). Please calculate function (n, k).INPUTInput starts with an integer T (t≤400), denoting the number of test cases.Each case contains the integers, N and K N (1≤n≤1018) represents the length of the strings, and K (1≤k≤9) Rep Resents the biggest digit in the string21 17 6OUTPUTFor each case, first output of the case number as ' case#x: ', and X is the case number. Then the output function (n, k) mod 20140518 in the case.Case #1:2Case #2:818503source:2014 ACM-ICPC Beijing Invitational Programming ContestTest Instructions: First define a string elegant string: Any substring in this string does not include a full permutation of 0~k, and now gives N and K, which requires the number of elegant string of length n to be calculated. Solution: dp+ Matrix fast power. We first use DP to launch the recursive formula, and then use the matrix to solve the equation quickly. Specific ideas are as follows:defines the DP array: dp[12][12] (Dp[i][j] indicates that the end of the string is the length of I when there are a number of J different numbers, example: 1233 at the end there is only one digit 3,2234 at the end of 3:2,3,4). take the second set of data as an example: N=7 k=6initialization: dp[1][1]=k+1,dp[1][2,,, k]=0;Dp[i+1][1]=dp[i][1]+dp[i][2]+dp[i][3]+dp[i][4]+dp[i][5]+dp[i][6]Dp[i+1][2]=6*dp[i][1]+dp[i][2]+dp[i][3]+dp[i][4]+dp[i][5]+dp[i][6]Dp[i+1][3]=5*dp[i][2]+dp[i][3]+dp[i][4]+dp[i][5]+dp[i][6]Dp[i+1][4]=4*dp[i][3]+dp[i][4]+dp[i][5]+dp[i][6]Dp[i+1][5]=3*dp[i][4]+dp[i][5]+dp[i][6]Dp[i+1][6]=2*dp[i][5]+dp[i][6]change the recursive style to the following matrix:and then we can solve it with a fast power.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cstdlib>5#include <cmath>6#include <algorithm>7 #defineINF 0x7fffffff8 using namespacestd;9typedefLong Longll;Ten Const intmaxn= A; One Constll mod=20140518; A - ll N,k; - structMatrix the { - ll AN[MAXN][MAXN]; - }a,b; - matrix Multiply (Matrix X,matrix y) + { - matrix sum; +memset (sum.an,0,sizeof(sum.an)); A for(intI=1; i<=k; i++.) at { - for(intj=1; j<=k; j + +) - { - for(intK2=1; k2<=k; k2++.) { -Sum.an[i][j]= (sum.an[i][j]+x.an[i][k2]*y.an[k2][j]%MoD); - if(SUM.AN[I][J]>=MOD) sum.an[i][j]-=MoD; in } - } to } + returnsum; - } the Matrix Power (ll K,matrix Q) * { $ Matrix temp;Panax Notoginseng for(intI=1; i<=k; i++.) - { the for(intj=1; j<=k; j + +) +temp.an[i][j]= i==J; A } the while(K) + { - if(k&1) temp=multiply (temp,q); $q=multiply (q,q); $K >>=1; - } - returntemp; the } - intMain ()Wuyi { the intT,ncase=1; -scanf"%d",&t); Wu while(t--) - { Aboutscanf"%lld%lld",&n,&k); $ if(n==1) - { -printf"Case #%d:%d\n", ncase++,k+1);Continue; - } A Matrix Q; + for(intI=1; i<=k; i++.) the { - for(intj=1; j<=k; j + +) $ { the if(j>=i) q.an[i][j]= (LL)1; the Else if(j==i-1) q.an[i][j]= (ll) (k +2-i); the ElseQ.an[i][j]= (LL)0; the } - } inQ=power (n1, q); the //for (int i=1; i<=k; i++) the // { About //for (int j=1; j<=k; j + +) the //cout<<q.an[i][j]<< ""; the //cout<<endl; the // } +ll ans=0; - for(intI=1; i<=k; i++.) the {BayiAns= (ans+ (LL) q.an[i][1]* (ll) (k +1))%MoD; the } theprintf"Case #%d:%lld\n", ncase++, ans); - } - return 0; the}
Follow-up: Thank you for your valuable comments ....
Bnuoj 34985 Elegant String