L. Poor God Water(ACM-ICPC 2018 焦作赛区网络预赛)

题意:有肉,鱼,巧克力三种食物,有几种禁忌,对于连续的三个食物,1.这三个食物不能都相同;2.若三种食物都有的情况,巧克力不能在中间;3.如果两边是巧克力,中间不能是肉或鱼,求方案数

题解:听其他队说用杜教BM 就可以过 ????  orz orz

我发现自己不一样啊!!!

我用的是矩阵快速幂,将meat ,  chocolate,fish 用 0 ,1 , 2 表示

对于 n 来说,我们只关注后两位,因为 若 n - 1 的所有方案解决的话,我们在 n - 1 的方案添加0, 1,2 就行,然后根据禁忌 去除不可能的

我们根据次状态 来更新现状态,然后矩阵快速幂

  

AC code:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int N=9;
struct Matrix{
    ll matrix[N][N];
};

const int mod = 1e9 + 7;

void init(Matrix &res)
{
    memset(res.matrix,0,sizeof(res.matrix));
    for(int i=0;i<N;i++)
        res.matrix[i][i]=1;
}
Matrix multiplicative(Matrix a,Matrix b)
{
    Matrix res;
    memset(res.matrix,0,sizeof(res.matrix));
    for(int i = 0 ; i < N ; i++){
        for(int j = 0 ; j < N ; j++){
            for(int k = 0 ; k < N ; k++){
                res.matrix[i][j] += a.matrix[i][k]*b.matrix[k][j];
                res.matrix[i][j] %= mod;
            }
        }
    }
    return res;
}
Matrix pow(Matrix mx,ll m)
{
    Matrix res,base=mx;
    init(res);
    while(m)
    {
        if(m&1)
            res=multiplicative(res,base);
        base=multiplicative(base,base);
        m>>=1;
    }
    return res;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ll n,ant = 0;
        scanf("%lld",&n);
        if(n == 1)  printf("3\n");
        else if(n == 2) printf("9\n");
        else
        {
            Matrix res,ans = {
                0,0,0, 1,0,0, 1,0,0,
                1,0,0, 0,0,0, 1,0,0,
                1,0,0, 1,0,0, 1,0,0,

                0,1,0, 0,1,0, 0,0,0,
                0,1,0, 0,0,0, 0,1,0,
                0,0,0, 0,1,0, 0,1,0,

                0,0,1, 0,0,1, 0,0,1,
                0,0,1, 0,0,0, 0,0,1,
                0,0,1, 0,0,1, 0,0,0
            };
            res=pow(ans,n-2);

            for(int i = 0;i < N;i++)
                for(int j = 0;j < N;j++)
                    ant = (ant + res.matrix[i][j]) % mod;
            printf("%lld\n",ant);
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/lemon-jade/p/9652036.html

时间: 2024-07-29 17:00:25

L. Poor God Water(ACM-ICPC 2018 焦作赛区网络预赛)的相关文章

Poor God Water(ACM-ICPC 2018 焦作赛区网络预赛 矩阵快速幂)

题目描述 God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him that some sequence of eating will make them poisonous.Every hour, God Water will eat one kind of food among meat, fish and chocolate. If there are

ACM-ICPC 2018 焦作赛区网络预赛 L 题 Poor God Water

God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him that some sequence of eating will make them poisonous. Every hour, God Water will eat one kind of food among meat, fish and chocolate. If there are 33 

ACM-ICPC 2018 焦作赛区网络预赛 H题 String and Times(SAM)

Now you have a string consists of uppercase letters, two integers AA and BB. We call a substring wonderful substring when the times it appears in that string is between AA and BB (A \le times \le BA≤times≤B). Can you calculate the number of wonderful

ACM-ICPC 2018 焦作赛区网络预赛 B题 Mathematical Curse

A prince of the Science Continent was imprisoned in a castle because of his contempt for mathematics when he was young, and was entangled in some mathematical curses. He studied hard until he reached adulthood and decided to use his knowledge to esca

ACM-ICPC 2018 焦作赛区网络预赛 K题 Transport Ship

There are NN different kinds of transport ships on the port. The i^{th}ith kind of ship can carry the weight of V[i]V[i] and the number of the i^{th}ith kind of ship is 2^{C[i]} - 12C[i]?1. How many different schemes there are if you want to use thes

ACM-ICPC 2018 焦作赛区网络预赛 L:Poor God Water(矩阵快速幂)

God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him that some sequence of eating will make them poisonous. Every hour, God Water will eat one kind of food among meat, fish and chocolate. If there are 3 c

ACM-ICPC 2018 焦作赛区网络预赛 L:Poor God Water(杜教BM)

有N个小时,有三种食物(用a ,b ,c代替好了),每个小时要吃一种食物,要求任意连续三个小时不能出现aaa,bbb,ccc,abc,cba,bab,bcb(假设b为巧克力) 的方案数 先用矩阵打表 先对两个数统计有1.aa 2.bb 3.cc 4.ab 5.ba 6.ac 7.ca 8.bc 9.cb 在添加一个数,则会有 若ba后面只能添加a或c 即形成新的两个字母组合1.aa 6.ac 即5可以产生1和6(以下代码不是照上面数字敲的) 以此类推即可产生新的a数组 ll a[2][15];

ACM-ICPC 2018 焦作赛区网络预赛 H、L

https://nanti.jisuanke.com/t/31721 题意 n个位置 有几个限制相邻的三个怎么怎么样,直接从3开始 矩阵快速幂进行递推就可以了 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const ll mod=1e9+7,maxn=9; 5 struct Matrix 6 { 7 ll m[maxn][maxn]; 8 Matrix() 9 { 10 memset(m

ACM-ICPC 2018 焦作赛区网络预赛 E. Jiu Yuan Wants to Eat (树链剖分-线性变换线段树)

树链剖分若不会的话可自行学习一下. 前两种操作是线性变换,模\(2^{64}\)可将线段树全部用unsigned long long 保存,另其自然溢出. 而取反操作比较不能直接处理,因为其模\(2^{64}\)的特殊性,可将其转化为线性变换. 显然 \[-x\equiv (2^{64}-1)*x (mod\ 2^{64})\] 因为\[!x = (2^{64}-1) -x \] 所以 \[ !x = (2^{64}-1) + (2^{64}-1)x\] #include<bits/stdc++