传送门

题目大意:

对于\(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. scanf格式控制符之%[]的应用

    考虑只读入小写字母的字符串,这个问题要如何用scanf解决呢? 这就用到了%[] 这个格式控制符,它支持a-z这样的格式控制 char s[111]; scanf("%[a-z]" ...

  2. An Introduction to Reflection in C++

    Apr 13, 2017 Stop me if you’ve heard this one before. You are working on a messaging middleware, a g ...

  3. AMD 的 CommonJS wrapping

    其实本文的标题应该是「为什么我不推荐使用 AMD 的 Simplified CommonJS wrapping」,但太长了不好看,为了美观我只能砍掉一截. 它是什么? 为了复用已有的 CommonJS ...

  4. vs2013载入zlib库,即include "zlib.h"

    转自wo13142yanyouxin原文vs2013载入zlib库,即include "zlib.h" 在程序中,我们经常要用到压缩,解压函数.以压缩函数compress为例进行说 ...

  5. 在网页里插入flash的代码

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://down ...

  6. 海量数据插入数据库效率对照測试 ---ADO.NET下SqlBulkCopy()对照LINQ 下InsertAllOnSubmit()

    摘要:使用.NET相关技术向数据库中插入海量数据是经常使用操作.本文对照ADO.NET和LINQ两种技术.分别使用SqlBulkCopy()和InsertAllOnSubmit()方法进行操作. 得出 ...

  7. MyBatis-Invalid bound statement (not found)-问题处理

    最近把工程改为Hibernate和MyBatis并存,并存只要注意两点即可: 1.使用同一个dataSource 2.事物交给Hibernate进行管理(Hibernate4+)  Hibernate ...

  8. js 简单getByClass得封装

    function getByClass(oParent,sClass){ var elems = oParent.getElementsByTagName("*"); var ar ...

  9. 每天一个命令 ls

    默认建立一个目录时初始硬连接数为2若该目录下有几个目录就会再增加几个连接数 ls -li 查看inode信息 ls -al 查看所有文件信息 ls -lR 查看目录下目录下的文件(递归显示目录下的文件 ...

  10. spring 官方文档

    英文 http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/ 翻译(1-6章) http:// ...