壘骰子
賭聖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;
}