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 ...
随机推荐
- 通过obs进行推流
我们除了通过ffmpeg进行推流外还可以使用OBS这个软件进行推流, 界面化工具,配置起来也方便 obs下载地址 obs的基本配置使用教程 这里需要注意的是在填写推流地址URL 的时候 有一个流秘钥 ...
- mysql5.5碰到的type= MyISAM报错问题
最近把mysql升级到5.5版本,发现type= MyISAM报错,网上查了一下原来MYSQL5.5.x 版本 不支持 TYPE=MyISAM 这样的语句了!!! MYSQL语句写法 TYPE=My ...
- Win7下安装Flash低版本
我把HKEY_LOCAL_MACHINE\SOFTWARE\Macromedia\FlashPlayer\SafeVersions中高于要装的版本的项目都删了,还是不行. 看了这个帖子后发现,原来64 ...
- 手机上最简洁的"云笔记"软件
❗️注意:该文并不是真的给你介绍各类云笔记的对比 第三方云笔记的缺点 以前用MIUI和Flyme时,自带的记事本很好用,小巧简洁,路上想起什么就写下来,回去后登录网上的个人中心,拿出来加工一下就可以发 ...
- XML教程!
什么是XML? XML是指可扩展标记语言(eXtensible Markup Language),它是一种标记语言,很类似HTML.它被设计的宗旨是传输数据,而非显示数据.XML标签没有被预定义,需要 ...
- bzoj 2648: SJY摆棋子 KDtree + 替罪羊式重构
KDtree真的很妙啊,真的是又好写,作用还多,以后还需更多学习呀. 对于这道题,我们求的是曼哈顿距离的最小值. 而维护的变量和以往是相同的,就是横纵坐标的最小值与最大值. 我们设为一个极为巧妙且玄学 ...
- 初见UDP_Server
from socket import *ip_prot = ('192.168.55.1',8080)buffer_size = 1024udp_sever = socket(AF_INET,SOCK ...
- 并发编程——全局解释器锁GIL
1.全局解释器锁GIL GIL其实就是一把互斥锁(牺牲了效率但是保证了数据的安全). 线程是执行单位,但是不能直接运行,需要先拿到python解释器解释之后才能被cpu执行 同一时刻同一个进程内多个线 ...
- 01 C#基础
第一天 .net平台(中国移动互联网平台): .net框架(信号塔): CLR(公共语言运行时) .Net类 库 我们使用的语言是——C# 2.解决方案项目与类的关系: 解决方案:公司 项目:部门 类 ...
- bitset优化背包
题目:https://agc020.contest.atcoder.jp/tasks/agc020_c 回忆下一题,是零一背包,主要的做法就是凑出最接近sum/2的价值,然后发现现在的背包的容量是20 ...