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 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 }

原文地址:https://www.cnblogs.com/F-Mu/p/11289127.html

时间: 2024-11-09 10:21:25

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

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];

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

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++

ACM-ICPC 2018 焦作赛区网络预赛 B. Mathematical Curse &lt;&lt; DP

题意 有n个数和m个运算符,按顺序选m个数进行运算,初值为k,问最后能得到的最大值是多少. 思路 dp[i][j]表示选到了第i个数时用了j个运算符,观察发现,一个数只能由他前一个状态的最大值或最小值转移过来(因为乘上一个负数会使最小的数变最大),所以我们同时维护最大最小. 然后转移就行了,需要注意$i-1>j$时是不合法的状态. 转移方程:$dp[i][j]=max(dp[i-1][j-1]\circ now[i],dp[i-1][j])(i<j)$ 代码 1 #include<bit