166’E Tetrahedron

You are given a tetrahedron. Let’s mark its vertices with letters A, B, C and D correspondingly.

An ant is standing in the vertex D of the tetrahedron. The ant is quite active and he wouldn’t stay idle. At each moment of time he makes a step from one vertex to another one along some edge of the tetrahedron. The ant just can’t stand on one place.

You do not have to do much to solve the problem: your task is to count the number of ways in which the ant can go from the initial vertex D to itself in exactly n steps. In other words, you are asked to find out the number of different cyclic paths with the length of n from vertex D to itself. As the number can be quite large, you should print it modulo 1000000007 (109 + 7).

Input
The first line contains the only integer n (1 ≤ n ≤ 107) — the required length of the cyclic path.

Output
Print the only integer — the required number of ways modulo 1000000007 (109 + 7).

Sample test(s)
input
2
output
3
input
4
output
21
Note
The required paths in the first sample are:

D - A - D
D - B - D
D-C-D
改编

题解

递推+矩阵乘法

代码

//歪鸡劈
//don't copy
//or you'll 滚蛋
//¥¥¥
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#define LL long long
#define maxm 100010
using namespace std;
LL getint()
{
LL w=,q=;char ch=getchar();
while((ch<''||ch>'')&&ch!='-')ch=getchar();
if(ch=='-')q=,ch=getchar();
while(ch>=''&&ch<='')w=w*+ch-'',ch=getchar();
return q?-w:w;
} long long a[][]={{},{,,,,},{,,,,},{,,,,},{,,,,}},b[][]={{},{,,,,},{,,,,},{,,,,},{,,,,}},n,c[][];
void Maxtrixquick_pow(int k)
{
int i,j,kk;
while(k>)
{
if(k&)
{
//b*=a; b=b*a
for(i=;i<=;i++)
{
for(j=;j<=;j++)
{
c[i][j]=;
for(kk=;kk<=;kk++)
c[i][j]+=b[i][kk]*a[kk][j];
}
}
for(i=;i<=;i++)
for(j=;j<=;j++)
b[i][j]=c[i][j]%;
}
//a*=a;
k>>=;
for(i=;i<=;i++)
{
for(j=;j<=;j++){
c[i][j]=;
for(kk=;kk<=;kk++)
c[i][j]+=a[i][kk]*a[kk][j];
}
}
for(i=;i<=;i++)
for(j=;j<=;j++)
a[i][j]=c[i][j]%;
}
}
int main()
{
freopen("nong.in","r",stdin);
freopen("nong.out","w",stdout);
n=getint();
Maxtrixquick_pow(n);
//printf("",b[1][1]);
cout<<b[][];
return ;
}

  

最新文章

  1. Gray Code
  2. MotionEvent常见值
  3. Delphi同步互斥总结
  4. chrome提供的功能
  5. Oracle02——oracle分页、子查询、集合运算、处理数据、创建和管理表和其他数据库对象
  6. TDX指标的理解与改造(价格到达指标线提醒)
  7. LeetCode算法题-Island Perimeter(Java实现)
  8. Java中,尽量相信自己,使用自己写的方法,不要使用底层提供的方法。都是坑。
  9. Eclipse导入web项目报错找不到HttpServletRequest解决方法
  10. AI学习---TensorFlow框架介绍[图+会话+张量+变量OP+API]
  11. grid - 隐式网格
  12. java遍历HashMap的高效方法
  13. jenkins第一次登陆,输入完密码之后,卡在了SetupWizard[jenkins]处
  14. pac (PAC(Proxy Auto Config) 是一个 Script;经由编写这个 Script,我们可以让系统判断在怎么样的情形下,要利用哪一台 Proxy 来进行联机。)
  15. Redis-SDS
  16. Maven构建项目时index.jsp文件报错
  17. LR的响应时间与使用IE所感受时间不一致的讨论(摘抄补充)
  18. [转] 深入理解Java G1垃圾收集器
  19. tensorflow: a Implementation of rotation ops (旋转的函数实现方法)
  20. python爬虫之趟雷

热门文章

  1. java虚拟机(三)--HotSpot 对象
  2. 02Struts2 环境搭建
  3. 第一节:EasyUI样式,行内编辑,基础知识
  4. iOS APP EuclidStudy Service Support
  5. 完善本地搭建的jekyll环境(Windows)
  6. 字符串匹配「 KMP 算法 」
  7. Python学习笔记(1)对象类型
  8. Python爬虫常用库安装
  9. Java:冒泡排序 | 二分查找
  10. MySQL 分库、分表