cf166e 在四面体上寻找路线数 递推,取模
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 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).
The first line contains the only integer n (1 ≤ n ≤ 107) — the required length of the cyclic path.
Print the only integer — the required number of ways modulo 1000000007 (109 + 7).
2
3
4
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 在四面体上寻找路线数 递推,取模的更多相关文章
- P1754 球迷购票问题 (卡特兰数,递推)
题目背景 盛况空前的足球赛即将举行.球赛门票售票处排起了球迷购票长龙. 按售票处规定,每位购票者限购一张门票,且每张票售价为50元.在排成长龙的球迷中有N个人手持面值50元的钱币,另有N个人手持面值1 ...
- Buy the Ticket(卡特兰数+递推高精度)
Buy the Ticket Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- ACM_数数有多少(第二类Stirling数-递推dp)
数数有多少 Time Limit: 2000/1000ms (Java/Others) Problem Description: 小财最近新开了一家公司,招了n个员工,但是因为资金问题,办公楼只有m间 ...
- LOJ 3090 「BJOI2019」勘破神机——斯特林数+递推式求通项+扩域
题目:https://loj.ac/problem/3090 题解:https://www.luogu.org/blog/rqy/solution-p5320 1.用斯特林数把下降幂化为普通的幂次求和 ...
- Codeforces 57C (1-n递增方案数,组合数取模,lucas)
这个题相当于求从1-n的递增方案数,为C(2*n-1,n); 取模要用lucas定理,附上代码: #include<bits/stdc++.h> using namespace std; ...
- BZOJ 3930: [CQOI2015]选数 递推
3930: [CQOI2015]选数 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pro ...
- HDU——2067小兔的棋盘(卡特兰数&递推DP)
小兔的棋盘 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- 3930: [CQOI2015]选数|递推|数论
题目让求从区间[L,H]中可反复的选出n个数使其gcd=k的方案数 转化一下也就是从区间[⌈Lk⌉,⌊Hk⌋]中可反复的选出n个数使其gcd=1的方案数 然后f[i]表示gcd=i的方案数.考虑去掉全 ...
- CF 717A Festival Organization——斯特林数+递推求通项+扩域
题目:http://codeforces.com/contest/717/problem/A 是 BJOI2019 勘破神机 的弱化版. 令 \( g[i] \) 表示长为 i .以 1 结尾的方案数 ...
随机推荐
- mysql启动失败一例
操作系统版本:Ubuntu 13.04 mysql 版本:mysql-server-5.5 现象:突然之前接到报告说数据库启不来了.第一时间查看硬盘空间,看是否硬盘满了.发现空间使用正常. root@ ...
- 如何在windows下使用pip安装
首先电脑已经安装好了python 找到python的安装目录,接着找到pip.exe,一般而言它会在Scripts文件夹下,我这里选择的是pip2.7.exe 接下来,win+r,输入cmd,回车打开 ...
- arcgis javascript api 事件的监听及移除
On Style Events 方式 var mapExtentChange = map.on("extent-change", changeHandler); function ...
- 基于Redis的INCR实现一个限流器
模式:计数器 计数器是 Redis 的原子性自增操作可实现的最直观的模式了,它的想法相当简单:每当某个操作发生时,向 Redis 发送一个 INCR 命令. 比如在一个 web 应用程序中,如果想知道 ...
- 课程设计小组报告——基于ARM实验箱的捕鱼游戏的设计与实现
课程设计小组报告--基于ARM实验箱的捕鱼游戏的设计与实现 一.任务简介 1.1 任务内容 捕鱼游戏这个项目是一个娱乐性的游戏开发,该游戏可以给人们带来娱乐的同时还可以给人感官上的享受,所以很受人们的 ...
- .Net修改网站项目调试时的虚拟目录(未验证)
有些项目需要在IIS发布的时候,将网站发布到虚拟目录,为了保持调试和发布的路径同一,一般会修改VS调试的虚拟目录 一.Web应用程序 Web应用程序的修改方式非常简单,在解决方案资源管理器->项 ...
- 关于Java并发编程的总结和思考
编写优质的并发代码是一件难度极高的事情.Java语言从第一版本开始内置了对多线程的支持,这一点在当年是非常了不起的,但是当我们对并发编程有了更深刻的认识和更多的实践后,实现并发编程就有了更多的方案和更 ...
- Linux系统学习之软件安装
一.源码包编译安装 由于计算机无法直接执行用高级语言编写的源程序,因此想要运行程序,就需要一种机制来让计算机识别,这样程序才可能运行起来.一般来说,计算机中存在解释型和编译型两种语言. 所谓解释型语言 ...
- KKT(Karush-Kuhn-Tucher)条件
在优化理论中,KKT条件是非线性规划(nonlinear programming)最佳解的必要条件.KKT条件将lagrange乘数法(Lagrange multipliers)中的等式约束优化问题推 ...
- PAT A1113 Integer Set Partition (25 分)——排序题
Given a set of N (>1) positive integers, you are supposed to partition them into two disjoint set ...