垒骰子
赌圣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消耗 < 2ms
请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。
所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意: main函数需要返回0
注意: 只使用ANSI C/ANSI C++ 标准,不要调用依赖于编译环境或操作系统的特殊函数。
注意: 所有依赖的函数必须明确地在源文件中 #include <xxx>, 不能通过工程设置而省略常用头文件。
提交时,注意选择所期望的编译器类型。
思路:设置dp[i][j]为第i个骰子朝上的面是j的方案数。我们可以先限制骰子的侧面是固定的。
显然有dp[i][j] = sigma(dp[i-1][k]) if(k->j == true).由于侧面方案数为4,那么在乘以4^n就可以了。
发现转移相同,用矩阵加速即可。
数据:
999 4
1 2
1 3
1 4
1 5
9999 4
1 2
1 3
1 4
4 5
1000000 6
1 2
3 4
4 5
2 6
1 6
2 3
214100641
486857096
978053875
dp代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <string>
#include <vector>
#include <stack>
#define CLR(a, b) memset(a, (b), sizeof(a))
#define ll o<<1
#define rr o<<1|1
#define fi first
#define se second
using namespace std;
typedef long long LL;
const int MOD = 1e9+7;
void add(LL &x, LL y) {x += y; x %= MOD;}
int a[7] = {0, 4, 5, 6, 1, 2, 3};
LL dp[2][7];
bool Map[7][7];
LL pow_mod(LL a, int n)
{
LL ans = 1LL;
while(n)
{
if(n & 1)
ans = ans * a % MOD;
a = a * a % MOD;
n >>= 1;
}
return ans;
}
int main()
{
int n, m; scanf("%d%d", &n, &m);
CLR(Map, false);
while(m--) {
int u, v; scanf("%d%d", &u, &v);
Map[u][v] = Map[v][u] = true;
}
for(int i = 1; i <= 6; i++) dp[1&1][i] = 1LL;
for(int i = 2; i <= n; i++) {
for(int j = 1; j <= 6; j++) {
dp[i&1][j] = 0LL;
for(int k = 1; k <= 6; k++)
if(!Map[a[j]][k]) add(dp[i&1][j], dp[(i-1)&1][k]);
}
}
LL ans = 0;
for(int i = 1; i <= 6; i++) add(ans, dp[n&1][i]);
cout << ans * pow_mod(4, n) % MOD << endl;
return 0;
}
矩阵加速代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <string>
#include <vector>
#include <stack>
#define CLR(a, b) memset(a, (b), sizeof(a))
#define ll o<<1
#define rr o<<1|1
#define fi first
#define se second
using namespace std;
typedef long long LL;
const int MOD = 1e9+7;
void add(LL &x, LL y) {x += y; x %= MOD;}
int a[7] = {0, 4, 5, 6, 1, 2, 3};
struct Matrix {
LL a[7][7];
};
Matrix multi(Matrix x, Matrix y)
{
Matrix z; CLR(z.a, 0);
for(int i = 1; i <= 6; i++)
{
for(int k = 1; k <= 6; k++)
{
if(x.a[i][k] == 0) continue;
for(int j = 1; j <= 6; j++)
add(z.a[i][j], x.a[i][k] * y.a[k][j] % MOD);
}
}
return z;
}
Matrix res, ori;
Matrix Pow(int n)
{
while(n)
{
if(n & 1)
res = multi(res, ori);
ori = multi(ori, ori);
n >>= 1;
}
}
bool Map[7][7];
LL pow_mod(LL a, int n)
{
LL ans = 1LL;
while(n)
{
if(n & 1)
ans = ans * a % MOD;
a = a * a % MOD;
n >>= 1;
}
return ans;
}
int main()
{
int n, m; scanf("%d%d", &n, &m);
CLR(Map, false);
while(m--) {
int u, v; scanf("%d%d", &u, &v);
Map[u][v] = Map[v][u] = true;
}
CLR(ori.a, 0LL); CLR(res.a, 0LL);
for(int i = 1; i <= 6; i++) {
res.a[i][i] = 1LL;
for(int j = 1; j <= 6; j++) if(!Map[i][a[j]])
ori.a[i][j]++;
} Pow(n-1);
LL ans = 0;
for(int i = 1; i <= 6; i++)
for(int j = 1; j <= 6; j++)
add(ans, res.a[i][j]);
cout << ans * pow_mod(4, n) % MOD << endl;
return 0;
}