焦作网络赛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 continuous hours when he eats only one kind of food, he will be unhappy. Besides, if there are 33 continuous hours when he eats all kinds of those, with chocolate at the middle hour, it will be dangerous. Moreover, if there are 33 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 NN 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 10000000071000000007.

Input

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

Each of the next TT lines contains an integer NN that shows the number of hours. (1 \le N \le 10^{10}1≤N≤1010)

Output

For each test case, output a single line containing the answer.

样例输入复制

3
3
4
15

样例输出复制

20
46
435170

题目来源

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

就是告诉你任意三个可以填的情况 推n个的时候有多少种情况

像这种前一种情况可以推后一种情况的 而且n还这么大的 可以考虑矩阵快速幂

建一个9*9的矩阵 每一种表示一个2位可填的状态 行表示前两个 列表示这前两个可推出的后两个

比如MCC是一种可行的 就在MC这行对应CC这列写1

快速幂可得所有情况

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<algorithm>
 5 #include<stack>
 6 #include<queue>
 7 #include<map>
 8 #include<vector>
 9 #include<cmath>
10 #include<cstring>
11 #include<set>
12 #include<stack>
13 //#include<bits/stdc++.h>
14 #define inf 0x7f7f7f7f7f7f7f7f
15 using namespace std;
16 typedef long long LL;
17
18 const LL mod = 1e9 + 7;
19 int t;
20 LL n;
21
22 struct Matrix {
23     LL a[10][10] = {{0}};
24     Matrix operator * (const Matrix &b)const {
25         Matrix ret;
26         for (int i = 1; i <= 9; i++) {
27             for (int j = 1; j <= 9; j++) {
28                 ret.a[i][j] = 0;
29                 for (int k = 0; k <= 9; k++) {
30                     ret.a[i][j] += a[i][k] * b.a[k][j];
31                     ret.a[i][j] %= mod;
32                 }
33             }
34         }
35         return ret;
36     }
37 };
38
39 Matrix ksm(Matrix a, LL x)
40 {
41     Matrix ret, k;
42     k = a;
43     ret = a;
44     x--;
45     while (x) {
46         if (x & 1) {
47             ret = ret * k;
48         }
49         x >>= 1;
50         k = k * k;
51     }
52     return ret;
53 }
54
55 int main()
56 {
57     cin >> t;
58     while (t--) {
59         cin >> n;
60         if (n == 1) {
61             cout << 3 << endl;
62         }
63         else if (n == 2) {
64             cout << 9 << endl;
65         }
66         else {
67             Matrix m;
68             m.a[1][4] = m.a[1][5] = 1;
69             m.a[2][6] = m.a[2][7] = 1;
70             m.a[3][8] = m.a[3][9] = 1;
71             m.a[4][2] = m.a[4][7] = 1;
72             m.a[5][3] = m.a[5][9] = 1;
73             m.a[6][1] = m.a[6][5] = 1;
74             m.a[7][3] = m.a[7][8] = m.a[7][9] = 1;
75             m.a[8][1] = m.a[8][5] = 1;
76             m.a[9][2] = m.a[9][6] = m.a[9][7] = 1;
77             m = ksm(m, n - 2);
78             LL f[10] = { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
79             LL ans[10] = { 0 };
80             for (int i = 1; i <= 9; i++) {
81                 for (int j = 1; j <= 9; j++) {
82                     ans[i] += m.a[i][j] * f[j];
83                     ans[i] %= mod;
84                 }
85             }
86
87             LL res = 0;
88             for (int i = 1; i <= 9; i++) {
89                 res = (res + ans[i]) % mod;
90             }
91             cout << res << endl;
92         }
93     }
94     return 0;
95 }

原文地址:https://www.cnblogs.com/wyboooo/p/9660831.html

时间: 2024-10-08 10:26:16

焦作网络赛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 3 c

【2018徐州网络赛】Morgana Net (矩阵快速幂)

给你一个n*n的矩阵A,和一个m*m的矩阵B(m%2==1) B是卷积核,让你用B对A做t次卷积运算,并且对于A中的每一个元素计算出来的值要模2,所以A最后会是一个01矩阵. 问你经过t此后,A中有多少个元素=1 1<=t<=1e9,1<=n<=8,1<=m<=n SOLUTION: 二维矩阵展成1维 那么做法就是把A矩阵变成一个1*(n*n)的一维向量,然后构造一个(n*n)*(n*n)的辅助矩阵 我们观察到对于A中的每一个元素,每一次卷积运算,所要求乘积的值的位置是

zoj 2974 Just Pour the Water矩阵快速幂

Just Pour the Water Time Limit: 2 Seconds      Memory Limit: 65536 KB Shirly is a very clever girl. Now she has two containers (A and B), each with some water. Every minute, she pours half of the water in A into B, and simultaneous pours half of the

【ZOJ 2974】Just Pour the Water(矩阵快速幂)

传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2974 题意 给出n个杯子与初始水量同时进行操作 将其中的水同时平均分入所指定的杯子 进行x次后 输出杯子剩余水量 刚拿到这个题,第一反应是递推找规律,但是因为每个杯子的初始水量是未知的,所以能找的只是每个杯子水量与其余杯子水量的关系. 但是看到了操作次数巨大,而且最多只有20个杯子,感觉可以用快速幂去做. 我们假设矩阵a[i][j]代表第i个杯子的水有a[i][j

2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6155 Subsequence Count 矩阵快速幂

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6155 题意: 题解来自:http://www.cnblogs.com/iRedBean/p/7398272.html 先考虑dp求01串的不同子序列的个数. dp[i][j]表示用前i个字符组成的以j为结尾的01串个数. 如果第i个字符为0,则dp[i][0] = dp[i-1][1] + dp[i-1][0] + 1,dp[i][1] = dp[i-1][1] 如果第i个字符为1,则dp[i][1

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

2018焦作网络赛 - Poor God Water 一道水题的教训

本题算是签到题,但由于赛中花费了过多的时间去滴吧格,造成了不必要的浪费以及智商掉线,所以有必要记录一下 题意:方格从1到n,每一格mjl可以选择吃鱼/巧克力/鸡腿,求走到n格时满足 1.每三格不可重复同一种食物 2.每三格均不同食物时中间格子不可吃巧克力 3.每三格前后两格不可同时吃巧克力 以上三个条件的方案数,n<1e10 太长不看版:打表+快速幂AC 长篇吐槽版 很显然的,设\(dp[n][i][j][k]\),走到第\(n\)格时第\(n-2\)格的食物是\(i\),第\(n-1\)的食物

2018ICPC焦作- Poor God Water 求递推式+矩阵快速幂

题目链接:https://nanti.jisuanke.com/t/31721 题意:一个孩子吃饭,有meat, fish 和 chocolate 三种食物可以选.要求连续三顿饭食物不能完全相同,鱼和肉的前一顿和后一顿不能都是巧克力,巧克力的左右两边不能同时出现鱼和肉. 思路:分九种情况,求出递推式,写出标准矩阵,用矩阵快速幂. 代码: #include <iostream> #include <stdio.h> #include <string.h> #define

焦作网络赛

J . Jessie and Justin want to participate in e-sports. E-sports contain many games, but they don't know which one to choose, so they use a way to make decisions. They have several boxes of candies, and there are ii candies in the i^{th}ith box, each