垒骰子
赌圣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 <xxx>, 不能通过工程设置而省略常用头文件。
提交时,注意选择所期望的编译器类型。
矩阵快速幂 :
(F11,F12,F13,F14,F15,F16);
其中 Fij 表示i个筛子时j朝上的状态数,很容易用 Fi-1 表示 fi 找到方阵
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <stack>
#include <map>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <vector>
#include <complex>
using namespace std;
typedef long long ll;
const ll mod = 1e9+7;
const double ep = 1e-8;
const int len = 8;
struct mat{
ll a[len][len];
};
mat mul(mat x,mat y)
{
mat ans;
memset(ans.a,0,sizeof(ans.a));
for(int i = 0; i < 6; i++)
for(int j = 0; j < 6; j++)
{
for(int k = 0; k < 6; k++)
{
ans.a[i][j] = (ans.a[i][j]%mod+(x.a[i][k]*y.a[k][j])%mod)%mod;
}
}
return ans;
}
mat matqm(mat x,ll n)
{
mat ans;
memset(ans.a,0,sizeof(ans.a));
for(int i = 0; i < 6; i++) ans.a[i][i] = 1;
while(n)
{
if(n%2) ans = mul(ans,x);
x = mul(x,x);
n/=2;
}
return ans;
}
int main()
{
mat tmp;
for(int i = 0; i < 6; i++)
for(int j = 0; j < 6; j++)
tmp.a[i][j] = 4;
ll n,m;
cin >> n >> m;
for(int i = 0; i < m; i++)
{
int x,y;
scanf("%d%d",&x,&y);
int x_ = x <= 3 ? x+3 : x-3;
int y_ = y <= 3 ? y+3 : y-3;
tmp.a[x-1][y_-1] = 0;
tmp.a[y-1][x_-1] = 0;
}
mat ans = matqm(tmp,n-1);
ll res = 0;
for(int i = 0; i < 6; i++)
for(int j = 0; j < 6; j++)
{
res = (res+(ll)4*ans.a[i][j]) %mod;
}
cout << res <<endl;
return 0;
}