[Bzoj 2004] [Hnoi2010] Bus bus line "pressure dp + matrix multiplication"

Source: Internet
Author: User
Tags mul

Title Link: BZOJ-2004

Problem analysis

See the topic is not at all. He immediately looked at the Ben of God's men.

Because of the p<=10, I think it is using the pressure. Each successive P-position is compressed into a P-bit 2 binary number, in which a total of k bits is 1, indicating that the K-position is the current stop position of a bus. It is important to note that the first bit of each State must be 1, so that there is no duplicate status. Each state can be transferred to some state on the right (moved by the first 1 of the current state). Both the initial and terminating states are preceded by a K-bit of 1. Transfer n-k times with matrices.

Code
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath > #include <algorithm>using namespace std;const int maxmap = 5, Mod = 30031;int N, K, P, Top, Rec;int L[MAXM Ap];int Calc (int Num) {int Cnt = 0;while (num) {++cnt; num-= num &-num;} return Cnt;} BOOL Check (int x, int y) {x = (x-(1 << (P-1))) << 1;int t = x ^ y;if (t-(t &-t) = = 0) return True;r Eturn false;} struct Matrix {int x, y, num[maxmap][maxmap];void setxy (int a, int b) {x = A; y = b;} void Clear (int xx) {for (int i = 1, i <= x; ++i) {for (int j = 1; J <= y; ++j) {num[i][j] = xx;}}} M0, MZ; Matrix Mul (Matrix A, Matrix B) {matrix Ret;ret. SetXY (a.x, B.Y); ret. Clear (0), for (int i = 1, i <= ret.x; ++i) {for (int j = 1, J <= Ret.y; ++j) {for (int k = 1; k <= a.y; ++k) {ret. NUM[I][J] + = a.num[i][k] * B.num[k][j];ret. NUM[I][J]%= Mod;}}} return ret;} Matrix Pow (Matrix A, int b) {matrix ret, f;f = A;ret. SetXY (f.x, F.Y); ret. Clear(0); for (int i = 1; I <= ret.x; ++i) ret. Num[i][i] = 1;while (b) {if (b & 1) ret = Mul (ret, f); b >>= 1;f = Mul (f, f);} return ret;} int main () {scanf ("%d%d%d", &n, &k, &p); Top = 0;for (int i = (1 << (P-1)), I <= (1 << P)-1; ++i) {if (Calc (i) = = K) {L[++top] = i; if (i = = (1 << P)-1-((1 << (P-K))-1) Rec = Top; }}mz. SetXY (top, top); Mz. Clear (0); M0. SetXY (1, TOP); M0. Clear (0); M0. Num[1][rec] = 1;for (int i = 1; I <= top; ++i) {for (int j = 1; j <= Top; ++j) {if (Check (L[i], l[j]) MZ. NUM[I][J] = 1;}} MZ = Pow (mz, n-k); M0 = Mul (M0, MZ);p rintf ("%d\n", M0. NUM[1][REC]); return 0;}

  

[Bzoj 2004] [Hnoi2010] Bus bus line "pressure dp + matrix multiplication"

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.