【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 ;
}
【CodeForces】166'E的更多相关文章
- 【Codeforces】Round #491 (Div. 2) 总结
[Codeforces]Round #491 (Div. 2) 总结 这次尴尬了,D题fst,E没有做出来.... 不过还好,rating只掉了30,总体来说比较不稳,下次加油 A:If at fir ...
- 【Codeforces】Round #488 (Div. 2) 总结
[Codeforces]Round #488 (Div. 2) 总结 比较僵硬的一场,还是手速不够,但是作为正式成为竞赛生的第一场比赛还是比较圆满的,起码没有FST,A掉ABCD,总排82,怒涨rat ...
- 【LeetCode】166. Fraction to Recurring Decimal 解题报告(Python)
[LeetCode]166. Fraction to Recurring Decimal 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingz ...
- 【CodeForces】601 D. Acyclic Organic Compounds
[题目]D. Acyclic Organic Compounds [题意]给定一棵带点权树,每个点有一个字符,定义一个结点的字符串数为往下延伸能得到的不重复字符串数,求min(点权+字符串数),n&l ...
- 【Codeforces】849D. Rooter's Song
[算法]模拟 [题意]http://codeforces.com/contest/849/problem/D 给定n个点从x轴或y轴的位置p时间t出发,相遇后按对方路径走,问每个数字撞到墙的位置.(还 ...
- 【CodeForces】983 E. NN country 树上倍增+二维数点
[题目]E. NN country [题意]给定n个点的树和m条链,q次询问一条链(a,b)最少被多少条给定的链覆盖.\(n,m,q \leq 2*10^5\). [算法]树上倍增+二维数点(树状数组 ...
- 【CodeForces】925 C.Big Secret 异或
[题目]C.Big Secret [题意]给定数组b,求重排列b数组使其前缀异或和数组a单调递增.\(n \leq 10^5,1 \leq b_i \leq 2^{60}\). [算法]异或 为了拆位 ...
- 【CodeForces】700 D. Huffman Coding on Segment 哈夫曼树+莫队+分块
[题目]D. Huffman Coding on Segment [题意]给定n个数字,m次询问区间[l,r]的数字的哈夫曼编码总长.1<=n,m,ai<=10^5. [算法]哈夫曼树+莫 ...
- 【CodeForces】906 D. Power Tower 扩展欧拉定理
[题目]D. Power Tower [题意]给定长度为n的正整数序列和模数m,q次询问区间[l,r]累乘幂%m的答案.n,q<=10^5,m,ai<=10^9. [算法]扩展欧拉定理 [ ...
随机推荐
- vue中的input使用e.target.value赋值的问题
很久不写博客了... vue中对表单的处理,相对原生js,增加了一个双向绑定的语法糖:v-model.官方文档里有一段: v-model 会忽略所有表单元素的 value.checked.select ...
- 判断点击第几个按钮JS代码的三种方法
方法一:使用下标实现<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...
- CAD使用GetxDataDouble读数据(网页版)
主要用到函数说明: MxDrawEntity::GetxDataDouble2 读取一个Double扩展数据,详细说明如下: 参数 说明 [in] LONG lItem 该值所在位置 [out, re ...
- vue组件---动态组件之多标签页面
首先看下效果图 代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> & ...
- 运行jar包的命令
windows下使用java -jar xxx.jar运行,linux下使用nohup java -jar xxx.jar & 如果想停止jar运行,ps -ef查看进程(进程多的话也可以加上 ...
- x shell 连接不上本地虚拟机
登陆虚拟机服务器 输入ipconfig查看ip 地址(如果提示命令不存在,输入 ip addr) 输出结果中看标记处是否出现ip地址.我的打开后这里是没有地址的 然后输入 vi /etc/syscon ...
- swing之单选框和复选框
import java.awt.Container; import java.awt.GridLayout; import javax.swing.*; public class num_1v ext ...
- hdu 1754 I Hate It(线段树水题)
>>点击进入原题测试<< 思路:线段树水题,可以手敲 #include<string> #include<iostream> #include<a ...
- DataFrame NaN 替换为零
一个DataFrame 其中有空值NaN,将其替换为0: df.fillna(0) 如果将第一列替换为0: df[1].fillna(0,inplace=True)
- 【01】CSS3 Gradient 分为 linear-gradient(线性渐变)和 radial-gradient(径 向渐变)(转)
CSS3 Gradient 分为 linear-gradient(线性渐变)和 radial-gradient(径 向渐变).而我们今天主要是针对线性渐变来剖析其具体的用法.为了更好的应用 CSS3 ...