传送门

题目大意:

对于\(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 最开始的更多相关文章

  1. 2016 华南师大ACM校赛 SCNUCPC 非官方题解

    我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...

  2. noip2016十连测题解

    以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...

  3. BZOJ-2561-最小生成树 题解(最小割)

    2561: 最小生成树(题解) Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1628  Solved: 786 传送门:http://www.lyd ...

  4. Codeforces Round #353 (Div. 2) ABCDE 题解 python

    Problems     # Name     A Infinite Sequence standard input/output 1 s, 256 MB    x3509 B Restoring P ...

  5. 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解

    题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...

  6. 2016ACM青岛区域赛题解

    A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  7. poj1399 hoj1037 Direct Visibility 题解 (宽搜)

    http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...

  8. 网络流n题 题解

    学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...

  9. CF100965C题解..

    求方程 \[ \begin{array}\\ \sum_{i=1}^n x_i & \equiv & a_1 \pmod{p} \\ \sum_{i=1}^n x_i^2 & ...

随机推荐

  1. mariadb设置utf8mb4

    1 . mysql真正的utf8是utf8mb4才是有效的utf8 a). mariaDB的设置方法 #vim /etc/my.conf [mysqld] character_set_server=u ...

  2. linux基础学习8

      管理主机每天任务: 查询登录档.追踪流量.监控用户使用主机状态.主机各项硬设备状态. 主机软件更新查询.其他使用者要求: 因此shell script 就必须要学啊,虽然可以说绝大部分shell能 ...

  3. 11i REP-3000: 启动 Oracle Toolkit 时发生内部错误.

    +---------------------------------------------------------------------------+ 应用对象程序库: Version : 11. ...

  4. EasyMvc入门教程-基本控件说明(3)时间线

    我们有时候经常看到如下的页面: 或者快递物流信息图标,那么利用EasyMvc如何实现呢?很简单,看下面的例子: @{ var data=new List<TimeLineItem>() { ...

  5. ylb:SQL 视图(View)基础

    ylbtech-SQL Server: SQL Server-SQL 视图(View)基础 SQL 视图(View)基础. 1,ylb:视图(View)基础返回顶部 -- ============== ...

  6. 一张图,关于 Bayes error rate,贝叶斯错误率等的分析

    造轮子是那帮搞研究的“科学家”干的事情(类似E&E里的explore),“工程师”的职责是利用已有的东西解决问题(类似E&E里的exploit). 其次,即使以工程师的角色解决业务问题 ...

  7. SpringMVC项目中web.xml中的节点载入顺序问题

    SpringMVC项目中web.xml中的节点载入顺序问题,之前以为web.xml中就是一些配置信息,和节点的顺序没有关系.后来才发现初始化时的载入顺序是和节点的顺序相关的. 完整的web.xml文件 ...

  8. VC++动态链接库(DLL)编程深入浅出(二)

    好,让我们正式进入动态链接库的世界,先来看看最一般的DLL,即非MFC DLL 上节给大家介绍了静态链接库与库的调试与查看,本节主要介绍非MFC DLL. 4.非MFC DLL 4.1一个简单的DLL ...

  9. unity3D中使用Socket进行数据通信(一)

    公司今年3D产品的工作中心主要集中在提高产品深度上,通过对竞争产品的分析,发现我们的缺陷在于多人在线与后台管理部分,多人在线使用unity自带的Network能够搞定,后台部分前段时间主要研究了下Sq ...

  10. js 考记忆力得小游戏

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...