2015 Sixth Blue Bridge Cup Group B I C ++ title

2015 Sixth Blue Bridge Cup Group B I C ++ title

Base dice

Winner atm old age obsessed base dice, the dice is a barrier on top of another, not crooked, to be built by piling square column.
After a long observation, atm we discovered the secret of stable dice: some figures close to the surface will repel each other!
Let's take a look at the dice specification: 1 is opposite the opposite is 4,2 5,3 opposite is 6.
Suppose there are m sets of mutual exclusion, that each set of two numbers faces close together, the dice can not be a stable base up.
atm I want to calculate how many different ways possible barrier dice.
The same for both base dice ways, and both ways when the corresponding numbers corresponding to the height of the die toward the same only.
Since the number of programs may be excessive, output a result of the mold 10 ^ 9 + 7.

Do not underestimate the number of dice atm oh ~

「输入格式」
第一行两个整数 n m
n表示骰子数目
接下来 m 行,每行两个整数 a b ,表示 a 和 b 数字不能紧贴在一起。

「输出格式」
一行一个数,表示答案模 10^9 + 7 的结果。
「样例输入」
2 1
1 2

「样例输出」
544

"Data range"
of 30% of the data: n <= 5
data for 60%: n <= 100
to 100% of the data: 0 <n <= 10 ^ 9, m <= 36

Resources for:
peak memory consumption <256M
the CPU consumption <2000ms

Please strictly follow the requirements of output, not superfluous to print something like: "Please enter ..." unwanted content.

All source code in a single file, through debugging, submit the copy source.

Note: main function needs to return 0
Note: Use only ANSI C / ANSI C ++ standard, do not call a special function depends on the build environment or operating system.
Note: All functions must rely explicitly #include in the source file, the project can not be omitted setting common header file.

When you submit, pay attention to select the desired type of compiler.

2015 was true problem was quite simple = - = finale title is the template title.
After doing several Zhenti found a law. Every time the difficulty of difficulty. Relatively simple = 2019 - 2020 = .. then . It should be more difficult to. . Here's relatively difficult for me aha's. . . .
2015 have to do Zhenti ninth title stuck. I think the true problem quite simple. Comes to my knowledge blind, oh wow. Fortunately, I persevered.

Fast power matrix, wow. In fact, a template to write really fast with power almost = - = ah ha ha ha happy, an afternoon ah ah ah

This question is the Word template that ah ha ha ha

I had been thinking about why you want to take the 4 ^ n, rather than the 5 ^ n, like a long time = - = later enumeration column a bit stupid ah good ah. There is a face can not be established, then forget it has a face, and then there are four faces can rotate, the more I miss the Kazakhstan
this last ride 4 ^ n where is the use of fast power. Multiplicative inverse.
Fast power matrix is changed to digital matrix operations. Then a unit matrix corresponding to the multiplicative inverse of a matrix, the number of bits in accordance with the number of dimensions to be obtained. When contacting the original matrix fast vague feeling of power. Then last semesters line after passage. (Although the teacher talked with did not speak the same)
before touching. I feel a thorough understanding of a lot simpler ah ha ha ha
happy

Then there's the operational matrix.
Provided dp [i] [j] j i represents the height of the upwardly embodiment of
this is a cumulative process, with only about one below
dp [i] [j] = sum (dp [i-1] [ j ]) six faces

1
T represents an element in the matrix T [i] [j] for the i-th surface and the j-th surface conflict
matrix A represents the elements A [i] [j] is a first dice, j facing the pendulum method.
Multiplying the matrix A and the matrix T represents one embodiment of the two dice
is needed to obtain n is multiplied by the number of dice have to n-1 times the program can be friends.
It is the desire to obtain the matrix T n-1 power. And then multiplied by the A matrix. Finally multiplicative inverse fast power demand at 4 ^ n-th power to get an answer.
Finally, the most cumulative maintenance ans can be friends
remember every time modulo oh ~

Code section:

#include <bits/stdc++.h>
typedef long long ll;
const int mod = 1e9 + 7;
using namespace std;

int fro[7] = {0, 4, 5, 6, 1, 2, 3};
ll ans;

struct matrix
{
	int n, m;
	ll a[7][7];
};

matrix matrix_unit()
{
	matrix res;
	res.n = 6;
	res.m = 6;
	for (int i = 1; i <= res.n; i++)
	{
		for (int j = 1; j <= res.m; j++)
		{
			if (i == j)
			{
				res.a[i][j] = 1;
			}
			else
			{
				res.a[i][j] = 0;
			}
		}
	}
	return res;
}

matrix matrix_mul(matrix A, matrix B)
{
	matrix C;
	C.n = A.n;
	C.m = B.m;
	for (int i = 1; i <= C.n; i++)
	{
		for (int j = 1; j <= C.m; j++)
		{
			C.a[i][j] = 0;
			for (int k = 1; k <= A.m; k++)
			{
				C.a[i][j] += A.a[i][k] * B.a[k][j] % mod;
			}
		}
	} 
	return C;
}

matrix matrix_pow(matrix A, int n)
{
	matrix res = matrix_unit();
	matrix temp = A;
	while (n)
	{
		if (n & 1)
		{
			res = matrix_mul(res, temp);
		}
		temp = matrix_mul(temp, temp);
		n >>= 1;
	}
	return res;
}

ll pow_mod(ll a, int n)
{
	ll res = 1;
	while (n)
	{
		if (n & 1)
		{
			res = res * a % mod;
		}
		a = a * a % mod;
		n >>= 1;
	}
	return res;
}

int main()
{
	int m, n;
	cin >> n >> m;
	matrix col;
	col.n = 6;
	col.m = 6;
	for (int i = 0; i < 7; i++)
	{
		for (int j = 0; j < 7; j++)
		{
			col.a[i][j] = 1;
		}
	}
	int u, v;
	for (int i = 0; i < m; i++)
	{
		scanf ("%d%d", &u, &v);
		col.a[fro[u]][v] = 0;
		col.a[fro[v]][u] = 0;
	}
	matrix res, base;
	for (int i = 1; i <= 6; i++)
	{
		base.a[1][i] = 1;
	}
	base.n = 1;
	base.m = 6;
	res = matrix_pow(col, n - 1);
	res = matrix_mul(base, res);
	
	for (int i = 1; i <= 6; i++)
	{
		ans = (ans + res.a[1][i]) % mod;
	}
	ans = ans * pow_mod(4, n) % mod;
	cout << ans << endl;
	return 0;
}
Published 50 original articles · won praise 2 · Views 1261

Guess you like

Origin blog.csdn.net/qq_44624316/article/details/104680280
Recommended