It's a problem of intellectual disability .
Title Link: http://acm.xidian.edu.cn/problem.php?id=1180
dp+ Matrix Fast Power
The problem is n 1e18, so the complexity is O (1) or O (LGN). The game when only see is DP, the feeling of complexity is too high, did not think of matrix to optimize, GG.
The first to define the state: Dp[i][j][k] is to take the first I bead color is j and at the end there is a number of k continuous beads,
Transfer equation:
When K=1, dp[i][j][k]=dp[i-1][is not the color of J [All K];
When k! =1, Dp[i][j][k]=dp[i-1][j][k-1].
However, both time complexity and space complexity are too large to be optimized.
Considering the value of J, Dp[i][j][k] must be the same for the same I and K (symmetry), so dp[i][j][0]=dp[i-1][j][all k]* (m-1),
Thus, the transfer matrix can be constructed
Matrix=[[m-1,m-1,..., m-1,m-1],[1,0,..., 0,0],[0,1,..., 0,0],..., [0,0,..., 1,0]].
The initial state is [1,0,0,..., 0,0]^ (-1).
(More amazing is that the compiler encountered a compiler error: Internal compiler error:segmentation Fault, the character is simply not too good =)
The code is as follows:
1#include <cstdio>2#include <cstring>3 #defineN 1004 #defineM 233335 using namespacestd;6typedefLong LongLL;7 LL n,m,k;8 structmatrix{9 LL Mp[n][n];Ten Matrix Mul (Matrix A) { One Matrix temp; A for(intI=0; i<k;++i) - for(intj=0; j<k;++j) { -temp.mp[i][j]=0; the for(intt=0; t<k;++t) { -LL r= (Mp[i][t]*a.mp[t][j])%M; -temp.mp[i][j]= (temp.mp[i][j]+r)%M; - } + } - returntemp; + } A }; at Matrix pow (Matrix E,matrix a,ll N) { - while(n) { - if(n&1) e=E.mul (A); -A=A.mul (A); -n>>=1; - } in returnE; - } to matrix E,a; + intMainvoid){ - for(intI=0; i<n;++i) e.mp[i][i]=1; the for(intI=1; i<n;++i) a.mp[i][i-1]=1; * while(~SCANF ("%lld%lld%lld",&n,&m,&k)) { $k--;Panax Notoginseng for(intI=0; i<k;++i) a.mp[0][i]=m-1; -Matrix Temp=pow (e,a,n-1); theLL ans=0; + for(intI=0; i<k;++i) ans= (ans+temp.mp[i][0])%M; Aans= (ans*m)%M; theprintf"%lld\n", ans); + } -}
It's a problem of intellectual disability.