题目描述

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 N 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.

输入

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≤10^10)

输出

For each test case, output a single line containing the answer.
 
题意:有肉,鱼,巧克力三种食物,有几种禁忌,对于连续的三个食物,1.这三个食物不能都相同;2.若三种食物都有的情况,巧克力不能在中间;3.如果两边是巧克力,中间不能是肉或鱼,求方案数。
分析:设巧克力是1,肉是2,鱼是3。则对于n个小时来说,n-2和n-1的食物可以推出n-1和n的食物,设i=(k-1)*3+j,其中k是第n-2小时吃的食物编号,j是第n-1小时吃的食物编号,例如第n-2小时吃了肉,第n-1小时吃了鱼,则a6是这种方案的种数。通过枚举可算出递推:a1=a4+a7;a2=a1+a4;a3=a1+a7;a4=a5+a8;a5=a2+a8;a6=a2+a5+a8;a7=a6+a9;a8=a3+a6+a9;a9=a3+a6。(左式为第n-1小时和n小时吃某两类事物的方案数,右式为第n-2小时和第n-1小时吃某两类食物的方案数)。则答案为一个9*1的全为1的矩阵(代表a1~a9)与一个9*9的矩阵的n-2(n>=3)次幂相乘(若ai=aj+ak,则res[i][j]和res[i][k]都为1)。

 1 #include <bits/stdc++.h>
2
3 #define maxn 10
4 #define mod 1000000007
5 #define inf 0x3f3f3f3f
6 #define start ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
7 #define ll long long
8 #define LL long long
9 using namespace std;
10
11 struct Mat {
12 ll mat[maxn][maxn];
13
14 Mat() {
15 memset(mat, 0, sizeof(mat));
16 }
17 };
18
19 int n = 9;
20
21 Mat operator*(Mat a, Mat b) {
22 Mat c;
23 memset(c.mat, 0, sizeof(c.mat));
24 int i, j, k;
25 for (k = 1; k <= n; k++) {
26 for (i = 1; i <= n; i++) {
27 if (a.mat[i][k] == 0) continue;//优化
28 for (j = 1; j <= n; j++) {
29 if (b.mat[k][j] == 0) continue;//优化
30 c.mat[i][j] = (c.mat[i][j] + (a.mat[i][k] * b.mat[k][j]) % mod) % mod;
31 }
32 }
33 }
34 return c;
35 }
36
37 Mat operator^(Mat a, ll k) {
38 Mat c;
39 int i, j;
40 for (i = 1; i <= n; i++)
41 for (j = 1; j <= n; j++)
42 c.mat[i][j] = (i == j);
43 for (; k; k >>= 1) {
44 if (k & 1)
45 c = c * a;
46 a = a * a;
47 }
48 return c;
49 }
50
51 int main() {
52 start;
53 int T;
54 cin >> T;
55 while (T--) {
56 ll n;
57 cin >> n;
58 if (n == 1)
59 cout << 3 << endl;
60 else if (n == 2)
61 cout << 9 << endl;
62 else {
63 Mat res;
64 res.mat[1][4] = res.mat[1][7] = 1;
65 res.mat[2][1] = res.mat[2][4] = 1;
66 res.mat[3][1] = res.mat[3][7] = 1;
67 res.mat[4][5] = res.mat[4][8] = 1;
68 res.mat[5][2] = res.mat[5][8] = 1;
69 res.mat[6][2] = res.mat[6][5] = res.mat[6][8] = 1;
70 res.mat[7][6] = res.mat[7][9] = 1;
71 res.mat[8][3] = res.mat[8][6] = res.mat[8][9] = 1;
72 res.mat[9][3] = res.mat[9][6] = 1;
73 Mat cnt = res ^(n - 2);
74 ll t = 0;
75 for (int i = 1; i <= 9; ++i)
76 for (int j = 1; j <= 9; ++j)
77 t = (t + cnt.mat[j][i]) % mod;
78 cout << t << endl;
79 }
80 }
81 return 0;
82 }

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

  1. ACM-ICPC 2018 焦作赛区网络预赛

    这场打得还是比较爽的,但是队友差一点就再过一题,还是难受啊. 每天都有新的难过 A. Magic Mirror Jessie has a magic mirror. Every morning she ...

  2. ACM-ICPC 2018 焦作赛区网络预赛- L:Poor God Water(BM模板/矩阵快速幂)

    God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him t ...

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

    God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him t ...

  4. ACM-ICPC 2018 焦作赛区网络预赛- G:Give Candies(费马小定理,快速幂)

    There are N children in kindergarten. Miss Li bought them NNN candies. To make the process more inte ...

  5. ACM-ICPC 2018 焦作赛区网络预赛J题 Participate in E-sports

    Jessie and Justin want to participate in e-sports. E-sports contain many games, but they don't know ...

  6. 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 th ...

  7. ACM-ICPC 2018 焦作赛区网络预赛 I题 Save the Room

    Bob is a sorcerer. He lives in a cuboid room which has a length of AA, a width of BB and a height of ...

  8. 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 won ...

  9. ACM-ICPC 2018 焦作赛区网络预赛 G题 Give Candies

    There are NN children in kindergarten. Miss Li bought them NN candies. To make the process more inte ...

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

    A prince of the Science Continent was imprisoned in a castle because of his contempt for mathematics ...

随机推荐

  1. 配置pip源

    1.使用配置文件配置文件[global]trusted-host=pypi.doubanio.comindex-url=https://pypi.doubanio.com/simple配置文件放置位置 ...

  2. 系统MySQL服务无法启动报错1067的四种解决方法win7系统MySQL服务无法启动报错1067的四种解决方法

        有些win7系统用户在登陆mysql或者重装mysql时出现"无法启动MYSQL服务,错误1067",而且重启.修复注册表都没办法解决问题.那么遇到MySQL服务无法启动问 ...

  3. Kubernetes GoRoutineMap工具包代码详解

    1.概述 GoRoutineMap 定义了一种类型,可以运行具有名称的 goroutine 并跟踪它们的状态.它防止创建具有相同名称的多个goroutine,并且在上一个具有该名称的 goroutin ...

  4. 5.5. Java并发工具类(如CountDownLatch、CyclicBarrier等)

    5.5.1 CountDownLatch CountDownLatch是一个同步辅助类,它允许一个或多个线程等待,直到其他线程完成一组操作.CountDownLatch有一个计数器,当计数器减为0时, ...

  5. 编码器 | 基于 Transformers 的编码器-解码器模型

    基于 transformer 的编码器-解码器模型是 表征学习 和 模型架构 这两个领域多年研究成果的结晶.本文简要介绍了神经编码器-解码器模型的历史,更多背景知识,建议读者阅读由 Sebastion ...

  6. 南洋才女,德艺双馨,孙燕姿本尊回应AI孙燕姿(基于Sadtalker/Python3.10)

    孙燕姿果然不愧是孙燕姿,不愧为南洋理工大学的高材生,近日她在个人官方媒体博客上写了一篇英文版的长文,正式回应现在满城风雨的"AI孙燕姿"现象,流行天后展示了超人一等的智识水平,行文 ...

  7. jvm中类和对象定义存储基础知识

    1 类文件数据结构类型 Class文件结构主要有两种数据结构:无符号数和表 •无符号数:用来表述数字,索引引用.数量值以及字符串等,比如 图1中类型为u1,u2,u4,u8分别代表1个字节,2个字节, ...

  8. 【Python&RS】遥感影像的像素坐标转地理坐标(仿射变换)

    ​         GDAL(Geospatial Data Abstraction Library)是一个在X/MIT许可协议下的开源栅格空间数据转换库.它利用抽象数据模型来表达所支持的各种文件格式 ...

  9. 如何洞察 C# 程序的 GDI 句柄泄露

    一:背景 1. 讲故事 前段时间有位朋友找到我,说他的程序界面操作起来很慢并且卡顿等一些不正常现象,从任务管理器看了下 GDI句柄 已经到 1w 了,一时也找不出什么代码中哪里有问题,让我帮忙看下,其 ...

  10. 深入探索C++对象模型(Inside the C++ object model) -- 摘阅笔记(关于对象 - esp 1)

    Object Lessons 关于对象 在C语言中,"数据"和"处理数据的操作(函数)"是分开声明的,也就是说 ,语言本身并没有支持"数据和函数&qu ...