As a second programming big problem, the difficulty is not as difficult as imagined. Only blame I did not seriously calm down to carefully understand the meaning of the topic. Even the sample did not understand, high number of classes idle to nothing, then calm down seriously thought about it.
The general title means to give you n dice, make you pile up, and tell you that there are some faces that cannot be combined. Ask you how many total programs you have in total. (Dice specify 1 and 4 relative, 2 and 5 relative, 3 and 6 relative)
Enter n m to indicate the number of dice and the number of items that cannot be combined.
Enter M line, two number a B for each line indicates that both sides of a and B cannot be together.
Output one line scheme number, the data is larger please die 100000000+7;
Sample input:
2 1
1 2
Sample output:
544
60% of the data is n<100.
100% of the data is n<10^9, m<36;
For 60% of the data, DP's idea is very good to think. The game has also been thought of. But because I did not think clearly of the example, so that the problem is complicated. Wrote a bunch of map balabala~.
After the correct understanding of the meaning of the topic, we can find that to continue to add a dice on the n-1 dice, we need to know the original first n-1 the top of the dice and the bottom of the current dice. Because the bottom surface of the dice can be known by the top side, the DP array needs to have a one-dimensional record top surface.
DP[I][J]: After placing the first I dice and the top point is the number of reversal of J.
At this point we can only consider that the dice and the i-1 dice will not conflict.
DP[I][J] + = dp[i-1][k] * * *; (The state K and the state J two dice will not conflict, multiply 4 because the dice to the top of the J scheme has 4 kinds);
DP[1][J] = 4; (There are 4 scenarios for initializing each top-facing);
Calculates the number of points opposite the dice. Observation lesson found that the number of each face and the opposite points are 3, that is, the opposite number of points = (when the previous points + 3)% 6; Of course, 3 of the points to be a special award.
Thus, we can pass 60% of the data.
For 100% of the data, the order of magnitude increased to 10^9. It's pretty scary, really. But to know the magnitude of such a large amount of complexity must not be O (n), and we can see in front of a recursive, then, take a senior words, "fools know is the Matrix fast power." (Weak weak to say, the game I also want to be a matrix fast power solution, but test instructions did not understand the correct, DP did not write out, fast Power also did not think. Otherwise, you can't push it out.
By the preceding DP equation we can know that the matrix of the answer is definitely a 1*6 matrix. So to use the matrix fast power, the other matrix how to be 6*6 it?
In fact, we can know the common 6*6 species transfer mode according to the transfer. and whether it can be transferred can be represented by a 6*6 matrix.
So now there's an answer matrix
A[] = {4,4,4,4,4,4}
A[i] Indicates the number of scenarios with vertices of I points
There is also a transfer matrix that is initially
B[][] = {
4,4,4,4,4,4,
4,4,4,4,4,4,
4,4,4,4,4,4,
4,4,4,4,4,4,
4,4,4,4,4,4,
4,4,4,4,4,4
}
The B-matrix means that the b[i][j] element has the number of points in the dice column with a J as the vertex of the dice. The resulting _a[6] matrix, which is multiplied by the 1*6 matrix, still represents the number of scenarios for each vertex.
For each group that cannot be combined, we just need to place the 0 in the corresponding position of the B array.
So ask the solution | a*b^ (n-1) | On the line.
#include <cstring> #include <iostream>using namespace std;const long Long MOD = 1000000000 + 7;int N, m;bool VI S[7][7];int solve_01 () {Long long Dp[110][7];memset (DP, 0, sizeof (DP)), for (int i=1; i<7; i++) {dp[1][i] = 4;} for (int i=2, i<=n; i++) {for (int j=1, j<=6; j + +) {for (int k=1; k<=6; k++) {int tmp = (j + 3)% 6;if (tmp = = 0) TMP = 6;if (vis[k][tmp]) {dp[i][j] = (Dp[i][j] + dp[i-1][k] * 4)% MOD;}}} Long long sum=0;for (int i=1; i<=6; i++) {sum = (sum + dp[n][i])% MOD;} return sum% MOD;} struct Node {long long a[7][7]; Node () {memset (A, 0, sizeof (a)), for (int i=0; i<7; i++) {a[i][i] = 1;}} void Show () {cout << "=====node=====" << endl;for (int i=1; i<=6; i++) {for (int j=1; j<=6; J + +) {cout & lt;< A[i][j] << "";} cout << Endl;}}; Node Mul (node A,node b) {node C; for (int i=1, i<=6; i++) {for (int j=1; j<=6; J + +) {c.a[i][j]=0; for (int v=1; v<=6; v++) {C.a[i][j] + = (a.a[i][v] * b.a[v][j])% MOD; } c.a[i][j]%=mod; }}return C; }node Power (node s, int n) {node res;while (n > 0) {if (n&1) res = Mul (res, s); s = Mul (s, s); n>>=1;} return res;} int solve_02 () {Node S;memset (S.A., 0, sizeof (s)), for (Int. I=1; i<=6; i++) {for (int j=1; j<=6; J + +) {if (Vis[i][j]) {int tmp = (j + 3)% 6;if (tmp = = 0) tmp = 6;s.a[i][tmp] = 4;}}} Node rec = Power (s, n-1); long long ans = 0;for (int i=1; i<=6; i++) {for (int j=1; j<=6; J + +) {ans = (ans + rec.a[i] [j] * 4)% MOD;}} return ans%mod;} int main () {memset (Vis, true, sizeof (VIS)), CIN >> N >> m;int A, b;for (int i=0; i<m; i++) {cin >> a >> b;vis[a][b] = false;vis[b][a] = false;} int res = SOLVE_01 (), int rec = solve_02 (), cout << res << "" << rec << endl;return 0;}
Blue Bridge Cup programming big question--dice problem