Sgu 197 nice patterns strike back | zoj 2317 nice patterns strike back

Source: Internet
Author: User

Question link ~~>

Question recognition:After doing this, I learned a lot. First, I learned about the state compression push formula.

Solution:

Constructor matrix:B [I] [J] (I, j is in the state, 0 <= I, j <= (1 <m)-1 ), if B [I] [J] is equal to 1, it indicates that status I is compatible with status J (I, j can be in two adjacent rows). If it is 0, it indicates that it cannot be adjacent, there is also a matrix of the original matrix A, (1 <m)-1 rows, and 1 columns, representing the number of solutions in various states in the first row, in this way, B * A gets the number of solutions in various states in the second line, so ~ > A ^ (n-1) * B obtains the number of solutions in the nth row. The final matrix is the total number of solutions. Because the index is relatively large, so ~> You need to simulate the division of 2 for a large number.

Code:

# Include <iostream> # include <sstream> # include <map> # include <cmath> # include <fstream> # include <queue> # include <vector> # include <sstream> # include <cstring> # include <cstdio> # include <stack> # include <bitset> # include <ctime> # include <string> # include <cctype> # include <iomanip> # include <algorithm> using namespace STD; # define int long int # define L (x) (x * 2) # define R (x) (x * 2 + 1) const int INF = 0x3f3f3f; const do Uble ESP = 0.0000000001; const double Pi = ACOs (-1.0); const int my = 1400 + 5; const int MX = 1005 + 5; int S, M; int d [MX]; int MOD; struct STR {int N, d [MX]; STR () {n = 0; memset (D, 0, sizeof (d);} STR operator-(int x) {STR s; S. N = N; For (INT I = n-1; I> = 0; -- I) if (d [I]) {d [I] --; for (Int J = I + 1; j <n; ++ J) d [J] = 9; break;} If (d [0] = 0) {for (INT I = 1; I <n; ++ I) s. d [I-1] = d [I]; S. N --;} else for (INT I = 0; I <n; ++ I) s. d [I] = d [I]; return s;} STR operator> (INT X) {STR s; int A [MX], num = 0, sum = 0; for (INT I = 0; I <n; ++ I) {sum = sum * 10 + d [I]; If (sum> = 2) {A [num ++] = sum/2; sum = sum % 2;} else if (Num) A [num ++] = 0 ;} for (INT I = 0; I <num; ++ I) s. d [I] = A [I]; S. N = num; return s;} bool operator & (int x) {If (d [n-1] & 1) Return True; return false ;}}; struct m {int P [33] [33]; m () {memset (p, 0, sizeof (p ));} void Init () {for (INT I = 0; I <s; ++ I) P [I] [I] = 1 ;}}; bool judge (INT X, int y) {for (INT I = 0; I <m-1; ++ I) if (X & (1 <I )) & (X & (1 <(I + 1) & (Y & (1 <I )) & (Y & (1 <(I + 1) | (! (X & (1 <I ))&&! (X & (1 <(I + 1 )))&&! (Y & (1 <I ))&&! (Y & (1 <(I + 1) return false; return true;} m pre () // constructor {M C; for (INT I = 0; I <s; ++ I) for (Int J = I; j <s; ++ J) if (Judge (I, j )) c. P [I] [J] = C. P [J] [I] = 1; return C;} m operator * (const M & A, const M & B) // matrix multiplication {M C; for (INT I = 0; I <s; ++ I) for (int K = 0; k <s; ++ K) if (. P [I] [k]) for (Int J = 0; j <s; ++ J) C. P [I] [J] = (C. P [I] [J] +. P [I] [k] * B. P [k] [J]) % MOD; return C;} m POW (m a, str k) // fast power {m B; B. init (); While (K. n) {If (K & 1) B = B * A; A = A * A; k = k> 1;} return B;} int main () {char STR [MX]; while (CIN> STR> m> mod) {S = (1 <m); // The total status of str s; for (INT I = 0; I <strlen (STR); ++ I) s. d [I] = STR [I]-'0'; S. N = strlen (STR); s = s-1; m c = pre (); m B = POW (C, S); int ans = 0; for (INT I = 0; I <s; ++ I) for (Int J = 0; j <s; ++ J) ans = (ANS + B. P [I] [J]) % MOD; cout <ans <Endl;} return 0 ;}

Sgu 197 nice patterns strike back | zoj 2317 nice patterns strike back

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.