【题解】T54037 最开始
传送门
题目大意:
对于\(a+ \frac 1{a^{}}=n\)求$a^{m}+ \frac 1{a^{m}} $,对\(10^9+7\)取模。
题目做法:
乍看此题,没有思路,但是如果用数学办法推导一下,就知道怎么做了。
记\(f(x)=a^x+ \frac 1{a^{x}}\)
显然当\((x>=y)\)时候\(f(x+y)=f(x)\times f(y)-f(x-y)\)
于是得到递推式:
\(f(x)=f(1)\times f(x-1)-f(x-2)\),矩阵快速幂即可。
关于这个逆元为什么不需要处理,是因为这个拆分的过程中,我们实际上没有进行无效的除法,我们得到的\(f(x-y)\)访问的直接就是那个取模以后的值。
上代码:
#include<iostream>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<vector>
#include<set>
#include<map>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<bitset>
#include<ctime>
using namespace std;
#define TMP template < class ins >
#define endl '\n'
#define RP(t,a,b) for(register int t=(a),edd=(b);t<=edd;t++)
#define ERP(t,a) for(register int t=head[(a)];t;t=e[t].nx)
#define DRP(t,a,b) for(register int t=(a),edd=(b);t>=edd;t--)
typedef long long ll;
TMP inline ins qr(ins tag){
char c=getchar();
ins x=0;
int q=0;
while(c<48||c>57)
q=c==45?-1:q,c=getchar();
while(c>=48&&c<=57)
x=x*10+c-48,c=getchar();
return q==-1?-x:x;
}
const ll mod=1e9+7;
const int maxn=3;
ll n;
int K;
struct mtx{
ll data[maxn][maxn];
mtx(){memset(data,0,sizeof data);}
inline ll* operator [](int x){
return data[x];
}
inline void unis(){
RP(t,1,2)
data[t][t]=1;
}
inline mtx operator *(mtx a){
mtx temp;
RP(t,1,2)
RP(i,1,2)
RP(k,1,2)
temp[t][i]=((temp[t][i]+data[t][k]*a[k][i])%mod)%mod;
return temp;
}
inline mtx operator *=(mtx a){
return (*this)=(*this)*a;
}
inline mtx operator ^(int x){
mtx ans,base=(*this);
ans.unis();
while(x){
if(x&1)
ans*=base;
base*=base;
x>>=1;
}
return ans;
}
inline mtx operator ^=(int x){
int p=x;
return (*this)=(*this)^p;
}
inline void print(){
RP(t,1,2){
RP(i,1,2)
cout<<(data[t][i])%mod<<' ';
cout<<endl;
}
cout<<endl;
}
};
int main(){
#ifndef ONLINE_JUDGE
freopen("in.in","r",stdin);
freopen("out.out","w",stdout);
#endif
n=qr(1ll);
K=qr(1);
mtx qaq;
qaq[1][1]=n;
qaq[1][2]=1;
qaq[2][1]=-1;
qaq[2][2]=0;
qaq^=K-1;
ll ans=((n*qaq[1][1])%mod+(2*qaq[2][1])%mod+mod)%mod;
cout<<ans<<endl;
return 0;
}
【题解】T54037 最开始的更多相关文章
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
- 2016ACM青岛区域赛题解
A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- poj1399 hoj1037 Direct Visibility 题解 (宽搜)
http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...
- 网络流n题 题解
学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...
- CF100965C题解..
求方程 \[ \begin{array}\\ \sum_{i=1}^n x_i & \equiv & a_1 \pmod{p} \\ \sum_{i=1}^n x_i^2 & ...
随机推荐
- java 相关软件使用趋势
http://www.baeldung.com/java-in-2017 https://mp.weixin.qq.com/s?__biz=MzI4NjYwMjcxOQ==&mid=224 ...
- 最新Webstrom, Idea 2019.1.3 的激活
1.注册码激活 打开网址(IntelliJ IDEA 注册码),我们能看到下面的界面,直接点击获取激活码,将生成的激活码粘贴到WebStorm激活对话框中的Lisence Code输入框,点击OK即可 ...
- mysqldump实践
mysqldump mysqldump---逻辑备份,热备 单线程,适合数据量小的库 mysql官方自带的命令行工具 #全库 )mysqldump -uroot -p123456 --sock ...
- 对象第复制operator=
类机制中有默认的对象复制操作符=,自定义对象复制需要注意一个问题,如果有遇到指针指向的资源是需要释放的,这时需要毫不留情释放,否则内存空间的泄露就不可避免.复制操作与拷贝构造函数的参数是一致的,只是在 ...
- 【Salvation】—— 项目策划&市场分析
写在前面:这个项目是2017年,我们评选校级创新基金项目的参加作品,小组4人,我为负责人,这个项目现在已经基本完成,目前处于后期收尾阶段. 一.项目的目标.内容及创新之处 1.研究目标 体现人类与自然 ...
- sql 从另外一张表查询数据存入本表. (有关联的)
UPDATE friends INNER JOIN users ON friends.friendid=users.userid SET friends.friendname=users.userna ...
- vue prop单向数据流
Prop 是单向绑定的:当父组件的属性变化时,将传导给子组件,但是反过来不会.这是为了防止子组件无意间修改了父组件的状态,来避免应用的数据流变得难以理解. 另外,每次父组件更新时,子组件的所有 pro ...
- template.js文档
参见GitHub:https://github.com/yanhaijing/template.js/ template.js简介: template.js 一款javascript模板引擎,简单,好 ...
- C语言重要概念汇总
作者:郭孝星 微博:郭孝星的新浪微博 邮箱:allenwells@163.com 博客:http://blog.csdn.net/allenwells Github:https://github.co ...
- 怎样给filter加入自己定义接口
.在Cfilter类的定义中实现Interface接口的函数的定义: //-----------------------Interface methods----------------------- ...