HDU 4332 state compression matrix acceleration DP thank sue God

Source: Internet
Author: User

Now there are countless 1*1*2 bricks to build a chimney with a length of N. A brick can be erected and put in a horizontal manner. The problem is that when the four bricks are flat and F, there are two situations in the question.

Practice: use 0 to indicate null, and 1 to indicate overwriting. After simulating the placement of bricks in each section, we can find that the number of 1 is always an even number, which can be used to simplify the process. Due to the special nature of placement, the section can be abstracted into an eight-bit binary number that can affect each other at the beginning and end. In this way, all situations are considered for judgment.

# Include <cstdio> # include <cstring> # define mod 1000000007 # define LMT 130 # define ll long //~ The priority is lower than the displacement. // The memory will burst when defined in the structure .. // less defined in main // (x> I) & 1 is used to determine. // The using namespace STD for matrix multiplication; Class matrix {public: ll mat [LMT] [LMT]; int n, m; void Init (void) {memset (MAT, 0, sizeof mat);} friend Matrix Operator * (Matrix &, matrix &) ;}; matrix Matt, Trans; int State [260], Lim = 1 <8; Matrix Operator * (Matrix & A, Matrix & B) {matrix TEM; TEM. N =. n; TEM. M = B. m; TEM. init (); For (int K = 0; k <. m; k ++) for (INT I = 0; I <. n; I ++) if (. mat [I] [k]) for (Int J = 0; j <B. m; j ++) TEM. mat [I] [J] = (TEM. mat [I] [J] +. mat [I] [k] * B. mat [k] [J]) % MOD; return TEM;} bool Yes (INT code) {int S = 0; while (CODE) {S + = Code & 1; code >>=1;} return S % 2 = 0;} ll can (int A, int B) {int St, I, j; bool end = false; if (A = 0) return B = 255? 1ll: 0; for (I = 0; I <8; I ++) {J = (I + 7) % 8; if (A & (1 <I) & (A & (1 <j) = 0) {ST = I; break ;}} for (I = sT; I! = ST | End = false; I = (I + 1) % 8) {end = true; j = (I + 1) % 8; if (0 = (A & (1 <I) {If (0 = (B & (1 <I) return 0 ;} else {If (A & (1 <j) & (B & (1 <I) & (B & (1 <j ))) I ++; else if (B & (1 <I) = 0) continue; else return 0 ;}} return 1ll ;} void Init (void) {Lim = 0; memset (State,-1, sizeof (State); For (INT I = 0; I <1 <8; I ++) if (Yes (I) State [I] = lim ++; trans. init (); Trans. M = trans. N = lim; For (INT I = 0; I <1 <8; I ++) for (Int J = I + 1; j <1 <8; j ++) If (State [I]! =-1 & State [J]! =-1) trans. mat [State [J] [State [I] = trans. mat [State [I] [State [J] = Can (I, j); Trans. mat [lim-1] [lim-1] = 2;} matrix POW (Matrix T, int N) {matrix TEM; TEM. M = TEM. N = lim; TEM. init (); For (INT I = 0; I <Lim; I ++) TEM. mat [I] [I] = 1; while (n) {If (N & 1) TEM = TEM * t; t = T * t; n >>= 1 ;} return TEM;} int main (void) {int T, I, n; Init (); scanf ("% d", & T); for (I = 1; I <= T; I ++) {scanf ("% d", & N); Matt = POW (trans, n); printf ("case % d: % i64d \ n ", I, Matt. mat [lim-1] [lim-1]);} return 0 ;}

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.