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 continuous hours when he eats only one kind of food, he will be unhappy. Besides, if there are 3 continuous hours when he eats all kinds of those, with chocolate at the middle hour, it will be dangerous. Moreover, if there are 3 continuous hours when he eats meat or fish at the middle hour, with chocolate at other two hours, it will also be dangerous.

Now, you are the doctor. Can you find out how many different kinds of diet that can make God Water happy and safe during NNN hours? Two kinds of diet are considered the same if they share the same kind of food at the same hour. The answer may be very large, so you only need to give out the answer module 1000000007.
Input

The fist line puts an integer T that shows the number of test cases. (T≤1000)

Each of the next T lines contains an integer N that shows the number of hours. (1≤N≤1010)
Output

For each test case, output a single line containing the answer.
样例输入

3
3
4
15

样例输出

20
46
435170

首先考虑2位 有1:mc  2:mf  3:cf 4:cm 5:fc 6:fm 7:cc 8:mm 9:ff;

所以第三位必须满足题意 则mc后面只能mc 则生成的新的2位是 4:cm7:cc

1生成4,7,;2生成5,6,9;.........;9生成5,6

所以可以写出以下打表代码

ll a[2][15];
int main(){
    int op=0;
    for(int i=1;i<=9;i++)
        a[op][i]=1;
    for(int i=3;i<=30;i++){
        op^=1;
        for(int j=1;j<=9;j++)
            a[op][j]=0;
        a[op][1]=(a[op^1][6]+a[op^1][8])%MOD;
        a[op][2]=(a[op^1][4]+a[op^1][6]+a[op^1][8])%MOD;
        a[op][3]=(a[op^1][5]+a[op^1][7])%MOD;
        a[op][4]=(a[op^1][1]+a[op^1][7])%MOD;
        a[op][5]=(a[op^1][2]+a[op^1][9])%MOD;
        a[op][6]=(a[op^1][2]+a[op^1][3]+a[op^1][9])%MOD;
        a[op][7]=(a[op^1][1]+a[op^1][5])%MOD;
        a[op][8]=(a[op^1][4]+a[op^1][6])%MOD;
        a[op][9]=(a[op^1][2]+a[op^1][3])%MOD;
        ll ans=0;
        for(int j=1;j<=9;j++){
            printf("%lld ",a[op][j]);
            ans=(ans+a[op][j])%MOD;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

所以由以上公式构建9维矩阵,矩阵快速幂求解

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 0x3f3f3f3f
#define mem(a) ((a,0,sizeof(a)))
typedef long long ll;
ll A,B,n;
struct matrix
{
    ll a[10][10];
};
matrix mutiply(matrix u,matrix v)
{
    matrix res;
    memset(res.a,0,sizeof(res.a));
    for(int i=0;i<9;i++)
        for(int j=0;j<9;j++)
            for(int k=0;k<9;k++)
                res.a[i][j]=(res.a[i][j]+u.a[i][k]*v.a[k][j])%MOD;
    return res;
}
matrix quick_pow(ll n)
{
    matrix ans,res;
    memset(res.a,0,sizeof(res.a));
    memset(ans.a,0,sizeof(ans.a));
    for(int i=0;i<9;i++)
        res.a[i][i]=1;
    ans.a[5][0]=ans.a[7][0]=1;
    ans.a[3][1]=ans.a[5][1]=ans.a[7][1]=1;
    ans.a[4][2]=ans.a[6][2]=1;
    ans.a[0][3]=ans.a[6][3]=1;
    ans.a[1][4]=ans.a[8][4]=1;
    ans.a[1][5]=ans.a[2][5]=ans.a[7][5]=1;
    ans.a[0][6]=ans.a[4][6]=1;
    ans.a[3][7]=ans.a[5][7]=1;
    ans.a[1][8]=ans.a[2][8]=1;
    while(n)
    {
        if(n&1) res=mutiply(res,ans);
        n>>=1;
        ans=mutiply(ans,ans);
    }
    return res;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
        ll n;
        scanf("%lld",&n);
        if(n==1) printf("3\n");
        else{
            ll pos=0;
            matrix ans=quick_pow(n-2);
            for(int i=0;i<9;i++)
                for(int j=0;j<9;j++)
                    pos=(pos+ans.a[i][j])%MOD;
            printf("%lld\n",pos);
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/9651533.html

时间: 2024-10-10 19:38:57

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

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 焦作赛区网络预赛 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题 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. Magical Girl Haze

262144K There are NN cities in the country, and MM directional roads from uu to v(1\le u, v\le n)v(1≤u,v≤n). Every road has a distance c_ici?. Haze is a Magical Girl that lives in City 11, she can choose no more than KK roads and make their distances

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 焦作赛区网络预赛G Give Candies(欧拉降幂)

题意:给你n个东西,叫你把n分成任意段,这样的分法有几种(例如3:1 1 1,1 2,2 1,3 :所以3共有4种),n最多有1e5位,答案取模p = 1e9+7 思路:就是往n个东西中间插任意个板子,所以最多能插n - 1个,所以答案为2^(n - 1) % p.直接套用模板 #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cmath

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 就行,然