vijosP1049 presents for Christmas Eve
Links: https://vijos.org/p/1049
Ideas
Fast Power + matrix conversion.
The transformation of M-matrix is regarded as a multiplication operation in a fast power, so that the result of k/m operation can be obtained by the time of O (log (k/m)), and then the remaining k%m matrix is converted to complement.
Code
1#include <iostream>2#include <cstring>3 #definefor (A,B,C) for (int a= (b); a<= (c); a++)4 using namespacestd;5 6 Const intMAXN = -+Ten;7 8 intop[ One][MAXN];9 int_HASH[MAXN],TMP[MAXN],TMP2[MAXN],ANS[MAXN];Ten intn,m,k; One A voidCalcint*a,int*B) {//according to B operation Change a -for (I,1, N) tmp2[i]=A[b[i]]; -for (I,1, N) a[i]=Tmp2[i]; the } - - intMain () { -Ios::sync_with_stdio (false); +Cin>>n>>m>>K; -for (I,1, M) for (J,1, N) cin>>Op[i][j]; + Afor (I,1, n) _hash[i]=ans[i]=i;//Init at -for (I,1, m) Calc (_hash,op[i]);//Calc_hash - - intt=k/m; K%=m; - -memcpy (&tmp,&_hash,sizeof(_hash));//tmp=1 in while(T) { - if(t&1) calc (ans,tmp);//ans*=tmp toCalc (tmp,tmp);//tmp*=tmp +t>>=1; - } thefor (I,1, K) calc (ans,op[i]); * $for (I,1, N) cout<<ans[i]<<" ";Panax Notoginseng return 0; -}
vijosP1049 presents for Christmas Eve