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 结尾的方案数 ...
随机推荐
- PHP Excel导入数据到MySQL数据库
数据导出已经有了,怎么能没有数据导入呢,同样使用TP5框架,首先需要下载phpexcel.zip,放到第三方类库目录vendor目录下,然后有一个页面可以让你选择要导入的Excel文件,然后点击导入按 ...
- Oracle导入程序Imp的使用详解
Oracle的导入实用程序(Import utility)允许从数据库提取数据,并且将数据写入操作系统文件.imp使用的基本格式:imp[username[/password[@service]]], ...
- 用scrapy爬取亚马逊网站项目
这次爬取亚马逊网站,用到了scrapy,代理池,和中间件: spiders里面: # -*- coding: utf-8 -*- import scrapy from scrapy.http.requ ...
- 难点--均方误差(MSE)和均方根误差(RMSE)和平均绝对误差(MAE)
MSE: Mean Squared Error 均方误差是指参数估计值与参数真值之差平方的期望值; MSE可以评价数据的变化程度,MSE的值越小,说明预测模型描述实验数据具有更好的精确度. MSE=1 ...
- Installation Oracle11gR2 RAC---常见报错处理
第六章.报错处理 6.1 root.sh脚本执行失败处理 安装grid时,执行rootsh脚本报错如下: Adding Clusterware entries to inittab USM drive ...
- 如何在Ubuntu18.04(Bionic Beaver)系统安装Teamviewer远程控制软件
首先,打开TeamViewer的下载页面,下载Debian/Ubuntu的Deb安装包. 这里有64位和32位安装包选项.可以在Terminal(终端)中输入uname -a 查看自己系统版本是64位 ...
- 分布式计算(五)Azkaban使用
在安装好Azkaban后,熟悉Azkaban的用法花了较长时间,也踩了一些坑,接下来将详细描述Azkaban的使用过程. 目录 一.界面介绍 二.Projects 1. 创建Command类型单一Jo ...
- Winform 基础一 panel
一 居上.居中.居下 二 添加子控件 三 适应不同分辨率 四 内容超出,显示滚动条 一 .居上.居中.居下 二.添加子页面 Form7 childFrm = new Form7(); childFrm ...
- 更换apt-get官方源为163源
更换apt-get官方源为163源 1.# mv /etc/apt/sources.list /etc/apt/sources.list.bak 2.覆盖原来源文件 tee /etc/apt/sour ...
- IIS导入.pfx证书时报错:"A specified logon session does not exist. It may already have been terminated."
在IIS中可以直接导入.pfx文件来给站点绑定https协议: 如果在导入.pfx文件时,不选择"Allow this certificate to be exported"的话 ...