计蒜客习题:垒骰子


问题描述

赌圣atm晚年迷恋上了垒骰子,就是把骰子一个垒在另一个上边,不能歪歪扭扭,要垒成方柱体。
经过长期观察,atm 发现了稳定骰子的奥秘:有些数字的面贴着会互相排斥!我们先来规范一下骰子:1 的对面是 4,2 的对面是 5,3 的对面是 6。假设有 m 组互斥现象,每组中的那两个数字的面紧贴在一起,骰子就不能稳定的垒起来。atm 想计算一下有多少种不同的可能的垒骰子方式。两种垒骰子方式相同,当且仅当这两种方式中对应高度的骰子的对应数字的朝向都相同。由于方案数可能过多,请输出模 10^9 + 7的结果。
不要小看了 atm 的骰子数量哦~
输入格式
第一行两个整数 n(1≤n≤10^9),m(0≤m≤36),n表示骰子数目,
接下来 m 行,每行两个整数 a,b,表示 a 和 b 数字不能紧贴在一起。
输出格式
一行一个数,表示答案模 10^9+7的结果。
样例输入
3 4
1 1
2 2
3 3
4 4
样例输出
10880


AC代码


#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;
}

猜你喜欢

转载自blog.csdn.net/liukairui/article/details/80033056