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