Poor God Water(ACM-ICPC 2018 焦作赛区网络预赛 矩阵快速幂)
题目描述
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.
输入
Each of the next T lines contains an integer N that shows the number of hours. (1≤N≤10^10)
输出
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 焦作赛区网络预赛 矩阵快速幂)的更多相关文章
- ACM-ICPC 2018 焦作赛区网络预赛
这场打得还是比较爽的,但是队友差一点就再过一题,还是难受啊. 每天都有新的难过 A. Magic Mirror Jessie has a magic mirror. Every morning she ...
- 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 ...
- 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 ...
- ACM-ICPC 2018 焦作赛区网络预赛- G:Give Candies(费马小定理,快速幂)
There are N children in kindergarten. Miss Li bought them NNN candies. To make the process more inte ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- ACM-ICPC 2018 焦作赛区网络预赛 G题 Give Candies
There are NN children in kindergarten. Miss Li bought them NN candies. To make the process more inte ...
- ACM-ICPC 2018 焦作赛区网络预赛 B题 Mathematical Curse
A prince of the Science Continent was imprisoned in a castle because of his contempt for mathematics ...
随机推荐
- [ 基于宝塔部署 ] 恋爱博客 -- Like_Girl 5.0
1)环境准备 云服务器 [ CentOS 7 ] 域名解析 love.daxiaoba.cool 宝塔面板 yum install -y wget && wget -O install ...
- SQL后半部和JDBC
SQL后半部 排序order by asc 升序desc 降序select *from 表名 order by 列名 asc ; select *from 表名 order by 列名 asc , 列 ...
- 用R来分析洛杉矶犯罪
由于微信不允许外部链接,你需要点击文章尾部左下角的 "阅读原文",才能访问文中链接. 洛杉矶市(Los Angeles)或"爵士乐的诞生地(The Birthplace ...
- 实例讲解Flink 流处理程序编程模型
摘要:在深入了解 Flink 实时数据处理程序的开发之前,先通过一个简单示例来了解使用 Flink 的 DataStream API 构建有状态流应用程序的过程. 本文分享自华为云社区<Flin ...
- 【.NET深呼吸】将XAML放到WPF程序之外
上一篇水文中,老周说了一下纯代码编写 WPF 的大概过程.不过,还是不够的,本篇水文中咱们还要更进一步. XAML 文件默认是作为资源打包进程序中的,而纯代码编写又导致一些常改动的东西变成硬编码了.为 ...
- Fabric架构详解
1 整体架构 2 运行架构 Fabric CA(可选) peer:主节点模块,负责存储区块链数据,运行维护链码 orderer:交易打包,排序模块 cryptogen:组织和证书等资料生成模块 con ...
- 手写RPC框架之泛化调用
一.背景 前段时间了解了泛化调用这个玩意儿,又想到自己之前写过一个RPC框架(参考<手写一个RPC框架>),于是便想小试牛刀. 二.泛化调用简介 什么是泛化调用 泛化调用就是在不依赖服务方 ...
- windows安全中心打不开
解决win11打不开安全中心的问题!!! 许多用户在最近都升级了Windows11系统,而且不少用户最近在使用Win11的时候发现自己打不开Windows安全中心 操作方法: 管理员权限打开Power ...
- nacos连接不上配置的坑
问题: 今天在使用nacos时,发现怎么样都连接不上配置 思路: 毋庸置疑这个肯定是配置问题,下面是我现在的配置 nacos: username: nacos password: nacos serv ...
- maven报错:不再支持源选项 5。请使用 6 或更高版本
问题描述 在执行命令 mvn compile 发生错误 D:\Github_NOTES\JavaWeb_Learning\02Java\JavaWeb\Code\Maven1>mvn clean ...