Bzoj 4000 matrix fast Power optimization DP

Source: Internet
Author: User

Build a matrix, run fast power

1 /**************************************************************2 problem:40003 user:idy0024 language:c++5 result:accepted6 time:32 Ms7 memory:836 KB8 ****************************************************************/9  Ten#include <cstdio> One#include <cstring> A #defineM 6 -   - structMatrix { theunsigned v[1<<m][1<<M]; -     Constunsigned *operator[](intIConst{returnv[i];} - }; -   + intN, M, p, K, bound; - intattack[3]; + intstat[1<<m], id[1<<M], Stot; AMatrixBase, dest; at   - voidMake_unit (Matrix &x) { -      for(intI=0; i<stot; i++ ) -          for(intj=0; j<stot; J + + ) -X.V[I][J] = i==J; - } inMatrixoperator*(ConstMatrix &a,ConstMatrix &b) { - Matrix C; to      for(intI=0; i<stot; i++ ) +          for(intj=0; j<stot; J + + ) { -C.V[I][J] =0; the              for(intk=0; k<stot; k++ ) *C.V[I][J] + = a[i][k]*B[k][j]; $         }Panax Notoginseng     returnC; - } theMatrix Mpow (Matrix A,intb) { + Matrix RT; A      for(Make_unit (RT); b; b>>=1, a=a*a) the         if(b&1) rt=rt*A; +     returnRT; - } $ intGetarea (intSinta) { $     intRT =0; -      for(intb=0; b<m; b++ ) { -         if((s>>b) &1 ) { the             intAA =A; -             if(b<k)WuyiAA >>= Kb; the             Else -AA <<= BK; WuAA &=bound; -RT |=AA; About         } $     } -     returnRT; - } - voidbuild () { AStot =0; +memset (ID,-1,sizeof(ID)); the      for(ints=0; s<=bound; s++ ) { -         if(Getarea (s,attack[1]) &s)Continue; $stat[stot]=s; theId[s] =Stot; thestot++; the     } the      for(ints1=0; s1<=bound; s1++ ) { -         if(id[s1]==-1)Continue; in          for(intS2=0; s2<=bound; s2++ ) { the             if(id[s2]==-1)Continue; the             if(Getarea (s1,attack[2]) &AMP;S2)Continue; About             if(Getarea (s2,attack[0]) &s1)Continue; the             Base. v[id[s1]][id[s2]] =1; the         } the     } + } - intMain () { thescanf"%d%d%d%d", &n, &m, &p, &k);BayiBound = (1&LT;&LT;M)-1; the      for(intI=0; i<3; i++ ) the          for(intj=0, O; j<p; J + + ) { -scanf"%d", &o); -Attack[i] = Attack[i] | (o<<j); the         } theattack[1] ^=1<<K; the build (); theDest = Mpow (Base, N-1 ); -unsigned ans =0; the      for(intI=0; i<stot; i++ ) the          for(intj=0; j<stot; J + + ) theAns + =Dest[i][j];94printf"%u\n", ans); the}
View Code

Bzoj 4000 matrix fast Power optimization DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.