【题目】垒骰子
赌圣atm晚年迷恋上了垒骰子,就是把骰子一个垒在另一个上边,不能歪歪扭扭,要垒成方柱体。
经过长期观察,atm 发现了稳定骰子的奥秘:有些数字的面贴着会互相排斥!
我们先来规范一下骰子:1 的对面是 4,2 的对面是 5,3 的对面是 6。
假设有 m 组互斥现象,每组中的那两个数字的面紧贴在一起,骰子就不能稳定的垒起来。
atm想计算一下有多少种不同的可能的垒骰子方式。
两种垒骰子方式相同,当且仅当这两种方式
中对应高度的骰子的对应数字的朝向都相同。
由于方案数可能过多,请输出模 10^9 + 7 的结果。
不要小看了 atm 的骰子数量哦~
「输入格式」
第一行两个整数 n m
n表示骰子数目
接下来 m 行,每行两个整数 a b ,表示 a 和 b 数字不能紧贴在一起。
「输出格式」
一行一个数,表示答案模 10^9 + 7 的结果。
「样例输入」
2 1
1 2
「样例输出」
544
「数据范围」
对于 30% 的数据:n <= 5
对于 60% 的数据:n <= 100
对于 100% 的数据:0 < n <= 10^9, m <= 36
资源约定:
峰值内存消耗 < 256M
CPU消耗 < 2000ms
请严格按要求输出,不要画蛇添足地打印类似:“请您输入…” 的多余内容。
所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意: main函数需要返回0
注意: 只使用ANSI C/ANSI C++ 标准,不要调用依赖于编译环境或操作系统的特殊函数。
注意: 所有依赖的函数必须明确地在源文件中 #include , 不能通过工程设置而省略常用头文件。
提交时,注意选择所期望的编译器类型。
【答案】
测试用例:
In:2 3
4 1
6 6
2 4、
4 1
5 3、
5 13
2 3
2 6
1 6
2 2
5 1
3 1
4 4
5 4
5 6
1 5
6 3
5 4
3 4、
58 1
1 5、
80 15
6 3
4 4
4 6
5 4
3 5
2 6
3 5
5 3
3 5
5 5
5 6
3 6
2 4
2 6
3 3、
95 2
6 6
2 1、
9058466 10
4 5
2 4
4 6
3 2
1 6
6 4
1 6
4 2
6 3
3 6、
99593115 3
4 4
3 3
3 5
Out:496、277504、346112、638851023、398736396、384752299、553435775、752053776
暴力算法
#include <iostream>
#define yu 1000000007
using namespace std;
long long n;
bool cannt[7][7];
int face[7] = { 0,4,5,6,1,2,3 };
long long ans = 0;
int res[10000007];
void dfs(long long k, int up) {
if (k == n) {
// ans = (ans++) % yu;
ans++;
return;
}
int down;
for (int i = 1; i < 7; i++) {
down = face[i];
if (cannt[up][down]) {
res[++k] = i;
dfs(k, i);
k--;
}
}
}
int main() {
for (int i = 0; i < 7; i++)
for (int j = 0; j < 7; j++)
cannt[i][j] = true;
int m, a, b;
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> a >> b;
cannt[a][b] = false;
cannt[b][a] = false;
}
res[0] = 0;
dfs(0, 0);
long long ji=1;
for (int i = 0; i < n; i++) {
ji *= 4;
}
cout << (ans*ji)%yu;
//cout << ans;
}
完美解法
#include <cstdio>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
const int D = 6, MOD = 1e9 + 7;
typedef long long int64;
typedef int64 mat[D][D];
mat U, V, I;
void matmul(mat a, mat b, mat c) {
mat t = { {0} };
for (int i = 0; i < D; ++i)
for (int j = 0; j < D; ++j)
for (int k = 0; k < D; ++k)
(t[i][j] += a[i][k] * b[k][j]) %= MOD;
memcpy(c, t, sizeof(t));
}
int main() {
// freopen("dice.in" , "r", stdin);
// freopen("dice.out", "w", stdout);
int n, m;
// scanf("%d%d", &n, &m), --n;
cin >> n >> m;
--n;
for (int i = 0; i < D; ++i)
for (int j = 0; j < D; ++j)
U[i][j] = 4;
for (int i = 1, a, b; i <= m; ++i) {
// scanf("%d%d", &a, &b), --a, --b;
cin >> a >> b;
--a; --b;
U[a][b < 3 ? (b + 3) : (b - 3)] = 0;
U[b][a < 3 ? (a + 3) : (a - 3)] = 0;
}
for (int i = 0; i < D; ++i)
V[i][i] = 4;
for (; n; n >>= 1, matmul(U, U, U))
n & 1 ? matmul(V, U, V), 0 : 0;
int64 ans = 0;
for (int i = 0; i < D; ++i)
for (int j = 0; j < D; ++j)
ans += V[i][j];
// printf("%d\n", (int)(ans % MOD));
cout << (int)(ans % MOD);
return 0;
}