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 ...
随机推荐
- ASP.NET在IIS 5/6上的运行模型(ISAPI)
IIS 5.X中的ASP.NET 实现了Web Server和ASP.NET App的分离. IIS作为Web Server运行在InetInfo.exe进程上.该进程是非托管的本地进程. ASP.N ...
- DB2数据库load出现SQL3508N问题
SQL3508N装入或装入查询期间,当存取类型为 "<文件类型>"的文件或路径时出错.原因码:"<原因码>".路径:"< ...
- depth peeling实现半透明
aaarticlea/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aH
- LayUI中实现上级下拉框动态加载下级下拉框js
js代码: var form = layui.form, layer = layui.layer; form.on("select(上级)", function(data){ va ...
- CentOS7上安装google谷歌浏览器
1.首先进入根目录,然后进入etc/yum.repos.d目录下,创建google-chrome.repo文件 cd / cd etc/yum.repos.d vim google-chrome. ...
- 注解实战BeforeMethed和afterMethed
package com.course.testng;import org.testng.annotations.AfterMethod;import org.testng.annotations.Be ...
- BZOJ 2402 陶陶的难题II (01分数规划+树剖+线段树+凸包+二分)
题目大意:略 一定范围内求最大值,考虑二分答案 设现在选择的答案是$mid$,$max \left \{ \frac{yi+qj}{xi+pj} \right \} \geq mid $ 展开可得,$ ...
- el-select 根据value查询其对应的label值
<el-form-item label="库位" prop="goodsLocationId" > <el-col :span="1 ...
- [WPF,XAML] 跳动的心
原文:[WPF,XAML] 跳动的心 没什么艺术细胞,原谅,原谅! <Canvas Width="0" Height="0"> <Canvas ...
- java实现支付宝电脑支付(servlet版本)
前期准备: 蚂蚁金融开放平台 进行登录操作 进入我的开放平台 在上方找到沙箱,进入沙箱(网络编程虚拟执行环境). 这里的RSA2密钥设置下,我已经设置好了,所以便有了支付宝公钥(公钥是对外公开的,私钥 ...