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后面只能m和c 则生成的新的2位是 4:cm和7:cc
即1生成4,7,;2生成5,6,9;.........;9生成5,6;
所以可以写出以下打表代码
ll a[][];
int main(){
int op=;
for(int i=;i<=;i++)
a[op][i]=;
for(int i=;i<=;i++){
op^=;
for(int j=;j<=;j++)
a[op][j]=;
a[op][]=(a[op^][]+a[op^][])%MOD;
a[op][]=(a[op^][]+a[op^][]+a[op^][])%MOD;
a[op][]=(a[op^][]+a[op^][])%MOD;
a[op][]=(a[op^][]+a[op^][])%MOD;
a[op][]=(a[op^][]+a[op^][])%MOD;
a[op][]=(a[op^][]+a[op^][]+a[op^][])%MOD;
a[op][]=(a[op^][]+a[op^][])%MOD;
a[op][]=(a[op^][]+a[op^][])%MOD;
a[op][]=(a[op^][]+a[op^][])%MOD;
ll ans=;
for(int j=;j<=;j++){
printf("%lld ",a[op][j]);
ans=(ans+a[op][j])%MOD;
}
printf("%lld\n",ans);
}
return ;
}
所以由以上公式构建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[][];
};
matrix mutiply(matrix u,matrix v)
{
matrix res;
memset(res.a,,sizeof(res.a));
for(int i=;i<;i++)
for(int j=;j<;j++)
for(int k=;k<;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,,sizeof(res.a));
memset(ans.a,,sizeof(ans.a));
for(int i=;i<;i++)
res.a[i][i]=;
ans.a[][]=ans.a[][]=;
ans.a[][]=ans.a[][]=ans.a[][]=;
ans.a[][]=ans.a[][]=;
ans.a[][]=ans.a[][]=;
ans.a[][]=ans.a[][]=;
ans.a[][]=ans.a[][]=ans.a[][]=;
ans.a[][]=ans.a[][]=;
ans.a[][]=ans.a[][]=;
ans.a[][]=ans.a[][]=;
while(n)
{
if(n&) res=mutiply(res,ans);
n>>=;
ans=mutiply(ans,ans);
}
return res;
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
ll n;
scanf("%lld",&n);
if(n==) printf("3\n");
else{
ll pos=;
matrix ans=quick_pow(n-);
for(int i=;i<;i++)
for(int j=;j<;j++)
pos=(pos+ans.a[i][j])%MOD;
printf("%lld\n",pos);
}
}
return ;
}
ACM-ICPC 2018 焦作赛区网络预赛 L:Poor God Water(矩阵快速幂)的更多相关文章
- ACM-ICPC 2018 焦作赛区网络预赛 L Poor God Water(矩阵快速幂,BM)
https://nanti.jisuanke.com/t/31721 题意 有肉,鱼,巧克力三种食物,有几种禁忌,对于连续的三个食物:1.这三个食物不能都相同: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 ...
- 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 焦作赛区网络预赛
这场打得还是比较爽的,但是队友差一点就再过一题,还是难受啊. 每天都有新的难过 A. Magic Mirror Jessie has a magic mirror. Every morning she ...
- 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 ...
随机推荐
- linux 下vim中关于删除某段,某行,或者全部删除的命令
- docker images镜像无法删除
删除所有容器: docker rm $(docker ps -aq) 删除镜像: docker rmi $(docker images -q) 如果有镜像无法删除,有可能更改了名字,用docker r ...
- 😈 HTTP 学习笔记
- POJ 1988 Cube Stacking( 带权并查集 )*
POJ 1988 Cube Stacking( 带权并查集 ) 非常棒的一道题!借鉴"找回失去的"博客 链接:传送门 题意: P次查询,每次查询有两种: M x y 将包含x的集合 ...
- 九、frp对外提供简单的文件访问服务
通过 static_file 插件可以对外提供一个简单的基于 HTTP 的文件访问服务.类似于http的文件索引! 服务端frps.ini配置[common]bind_addr = 0.0.0.0bi ...
- 小学生都能学会的python(字典{ })
小学生都能学会的python(字典{ }) 1. 什么是字典 dict. 以{}表示. 每一项用逗号隔开, 内部元素用key:value的形式来保存数据 {"jj":"林 ...
- CURL库的宏定义列表
列表CURL库一共同拥有17个函数 curl_close:关闭CURL会话 curl_copy_handle:复制一个CURL会话句柄,同一时候3复制其全部參数 curl_errno:返回最后一个错误 ...
- UIScrollView加入控件,控件距离顶部始终有间距的问题
今天.特别郁闷.自己定义了一个UIScrollView,然后在它里面加入控件,如UIButton *button = [[UIButton alloc] initWithFrame:CGRectMak ...
- LIVE555研究之五:RTPServer(二)
port是一样的. DynamicRTSPServer 继承关系: Medium是非常多类的基类.内部定义了指向环境类的引用和一个char类型媒体名称.并定义了依照媒体名称,查找相应媒体的成员函数lo ...
- C++模板中的静态
#include <iostream> #include <stdlib.h> using namespace std; template<class T> cla ...