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 结尾的方案数 ...
随机推荐
- Python学习—Pycharm连接mysql服务器
安装pymysql pip3 install pymysql 安装Mysql客户端驱动(基于Pycharm工具) 点击download,下载mysql驱动 等待驱动安装成功后,点击OK即可 创建数据库 ...
- Django框架的使用教程--Cookie-Session[五]
Cookie cookie是存储在浏览器中的一段文本信息,下次同一网站请求,就会发送该cookie给服务器,一般的浏览器都有启动cookie,用cookie存储信息,最好不要存储密码,cookie也有 ...
- Linux 小知识翻译 - 「协议(protocol)」
对于理解服务器和网络来说,「协议」是不可缺少的概念. 「协议(protocol)」有「规则,规定」的意思. 实际上「协议」的函数很广,在通信领域,「协议」规定了「在通信时,什么样的情况下,以什么样的顺 ...
- ABAP 中JSON格式的转换与解析
RT,JSON是当今十分流行的一种轻量数据格式,广泛地应用于各种数据交换场景中.本文会介绍一种比较简单的将ABAP中的数据转换为JSON格式的方法. (如果你是因为引号的问题搜索到了这篇文章,请直接拉 ...
- [HAOI2017]供给侧改革
题目 这道题我们其实就是利用了一棵后缀树 由于字符串是随机的,所以这个后缀树的树高是\(log\)的,基于树高的算法是能过的 我们考虑后缀树上的两个节点的\(lca\)就是这两个节点所代表的后缀的\( ...
- CSS3渐变——径向渐变
上节在<再说CSS3渐变——线性渐变>和大家一起学习了CSS3 Gradient中径向渐变最新语法(称得上是W3C的标准语法)相关知识以及其基本使用.今天我们在这一篇中主要和大家一起来了解 ...
- css3 媒体查询常用适配
@media (max-width:1300px) {} @media (max-width:1080px) {} @media (max-width:799px) {} @media (max-wi ...
- win10下SVN图标不显示
win10系统的SVN图标不现实了.正常情况下,会在文件夹上有一个对勾 但是对勾以及所有的SVN图标都突然消失了,都不知道文件什么状态了. 经过一通搜索,发现问题所在(都指向注册表图标被占用).原因就 ...
- kafka监控kafka-eagle 容器化配置
由于kafka.zk 集群已经部署在k8s中, kafka的服务名 kafka-hs, zk的服务名为:zk-cs ,对kafka进行监控,所以需要把监控部署到k8s中,选择使用kafka-eagl ...
- day20 Python 装饰器
装饰器:本质就是函数,为其他函数添加附加功能,一个原则是不修改函数的源代码,另外一个原则是不修改被修饰函数的调用功能 装饰器=高阶函数+函数嵌套+闭包 前戏 import time def cal(l ...