来源:codeforces                 E. Tetrahedron
 

You are given a tetrahedron. Let's mark its vertices with letters ABC 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 Dto 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).

Examples
input

Copy
2
output
3
input

Copy
4
output
21

思路:
递推ans[n]=ans[n-1]*2+ans[n-2]*3,但是ans会很大,需要取模
取模后的ans可能已经不是ans了
(ans[n-1]%mod*2+ans[n-2]*3)%mod==(ans[n-1]*2+ans[n-2]*3)%mod 是否成立? 经过查阅

  1.(a*b) mod M=(a mod M)*(b mod M) mod M

  2.(a+b) mod M=(a mod M+b mod M) mod M;



#include<bits/stdc++.h>
using namespace std;
int main()
{
long long n,ans,a=,b=,c=;
cin>>n;
if(n<)
{
if(n==)ans=a;
else if(n==)ans=b;
else if(n==)ans=c;
}
for(int i=;i<=n;i++)
{
ans=(b*+c*)%;
a=b;
b=c;
c=ans;
}
cout<<ans<<endl;
return ;
}

cf166e 在四面体上寻找路线数 递推,取模的更多相关文章

  1. P1754 球迷购票问题 (卡特兰数,递推)

    题目背景 盛况空前的足球赛即将举行.球赛门票售票处排起了球迷购票长龙. 按售票处规定,每位购票者限购一张门票,且每张票售价为50元.在排成长龙的球迷中有N个人手持面值50元的钱币,另有N个人手持面值1 ...

  2. Buy the Ticket(卡特兰数+递推高精度)

    Buy the Ticket Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...

  3. ACM_数数有多少(第二类Stirling数-递推dp)

    数数有多少 Time Limit: 2000/1000ms (Java/Others) Problem Description: 小财最近新开了一家公司,招了n个员工,但是因为资金问题,办公楼只有m间 ...

  4. LOJ 3090 「BJOI2019」勘破神机——斯特林数+递推式求通项+扩域

    题目:https://loj.ac/problem/3090 题解:https://www.luogu.org/blog/rqy/solution-p5320 1.用斯特林数把下降幂化为普通的幂次求和 ...

  5. Codeforces 57C (1-n递增方案数,组合数取模,lucas)

    这个题相当于求从1-n的递增方案数,为C(2*n-1,n); 取模要用lucas定理,附上代码: #include<bits/stdc++.h> using namespace std; ...

  6. BZOJ 3930: [CQOI2015]选数 递推

    3930: [CQOI2015]选数 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pro ...

  7. HDU——2067小兔的棋盘(卡特兰数&递推DP)

    小兔的棋盘 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  8. 3930: [CQOI2015]选数|递推|数论

    题目让求从区间[L,H]中可反复的选出n个数使其gcd=k的方案数 转化一下也就是从区间[⌈Lk⌉,⌊Hk⌋]中可反复的选出n个数使其gcd=1的方案数 然后f[i]表示gcd=i的方案数.考虑去掉全 ...

  9. CF 717A Festival Organization——斯特林数+递推求通项+扩域

    题目:http://codeforces.com/contest/717/problem/A 是 BJOI2019 勘破神机 的弱化版. 令 \( g[i] \) 表示长为 i .以 1 结尾的方案数 ...

随机推荐

  1. Apache的配置详解 带图

    对Apache 的 Http.conf 各项配置详解 1.01 ServerRoot 配置 [ServerRoot "" 主要用于指定 Apache 的安装路径,此选项参数值在安装 ...

  2. oracle中如何只查询一条复合条件的记录,即查到一条记录就返回(转)

    可以用rownum来查询一条记录. 如emp表中有如下数据. 要求查询deptno为20的,但只取一条记录,可用如下语句: select * from emp where deptno=20 and  ...

  3. 【PAT】B1073 多选题常见计分法(20 分)

    此处为我的存储结构,只提供一种思路,二维数组存储所有数据 #include<stdio.h> #include<string.h> #include<map> #i ...

  4. 路由交换02-----ARP协议

    路由交换协议-----ARP ARP协议 ARP(Address Resolution Protocol),是根据IP地址获取MAC地址的一个TCP/IP协议,即将IP地址对应到物理地址,从而实现数据 ...

  5. Node.js webpack Vue-CLI --安装

    Node.js 安装 从官网 下载 安装 Node.js 官网 Node.js 官方文档 cmd 命令 node -v 查看版本号 v10.15.0 npm 包管理工具 npm 是JavaScript ...

  6. 函数中声明变量不用Var的情况

    我们都知道函数中声明变量不用Var时这个变量会成为全局变量,但是并不是函数一开始执行就会把它变为全局变量,必须执行到这条语句. 看一段代码 function f(){    alert(a);    ...

  7. UVA11853-Paintball(对偶图)

    Problem UVA11853-Paintball Accept:229  Submit:1830 Time Limit: 3000 mSec Problem Description You are ...

  8. nodejs-stream部分

    参考: https://blog.csdn.net/eeewwwddd/article/details/81042225 http://nodejs.cn/api/stream.html#stream ...

  9. Java/JSP程序连接不上Mysql驱动问题解决方法

    错误提示: java.lang.ClassNotFoundException: com.mysql.jdbc.Driverat java.net.URLClassLoader$1.run(URLCla ...

  10. view 的用法

    MATLAB提供了设置视点的函数view.其调用格式为:       view(az,el)az是azimuth(方位角)的缩写,EL是elevation(仰角)的缩写.它们均以度为单位.系统缺省的视 ...