Jzzhu and Sequences CodeForces
来源:互联网 发布:linux系统的卸载命令 编辑:程序博客网 时间:2024/05/18 03:05
Jzzhu has invented a kind of sequences, they meet the following property:
You are given x and y, please calculate fn modulo1000000007 (109 + 7).
The first line contains two integers x andy (|x|, |y| ≤ 109). The second line contains a single integern (1 ≤ n ≤ 2·109).
Output a single integer representing fn modulo1000000007 (109 + 7).
Input
2 33
Output
1
Input
0 -12
Output
1000000006
In the first sample, f2 = f1 + f3,3 = 2 + f3,f3 = 1.
In the second sample, f2 = - 1; - 1 modulo (109 + 7) equals(109 + 6).
思路:构建矩阵
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef long long ll;const int mod=1e9+7;ll x,y;ll f[2][2];ll tmp[2][2];ll res[2][2],b[2][2];void multi(ll f[][2],ll b[][2],ll n){ memset(tmp,0,sizeof(tmp)); for(int i=0;i<2;i++) for(int j=0;j<2;j++) for(int k=0;k<2;k++) tmp[i][j]=((tmp[i][j]+f[i][k]*b[k][j])%mod+mod)%mod; for(int i=0;i<2;i++) for(int j=0;j<2;j++) f[i][j]=tmp[i][j];}void Pow(ll n){ memset(res,0,sizeof(res)); for(int i=0;i<2;i++) res[i][i]=1; while(n) { if(n&1) multi(res,f,n); multi(f,f,n); n>>=1; }}int main(){ ll n; while(~scanf("%lld%lld",&x,&y)) { f[0][0]=f[1][0]=1; f[0][1]=-1;f[1][1]=0; scanf("%lld",&n); if(n==1) { if(x<0) { printf("%lld\n",x+mod); } else printf("%lld\n",x); continue; } Pow(n-2); ll ans; ans=res[0][0]*y%mod+res[0][1]*x%mod; printf("%lld\n",(ans%mod+mod)%mod); } return 0;}
阅读全文
0 0
- Jzzhu and Sequences CodeForces
- Jzzhu and Sequences CodeForces
- Jzzhu and Sequences CodeForces
- Jzzhu and Sequences CodeForces
- codeforces B. Jzzhu and Sequences
- codeforces-DIV2-B- Jzzhu and Sequences
- Codeforces 450 B. Jzzhu and Sequences
- Codeforces Jzzhu and Sequences(循环节)
- Codeforces 450B Jzzhu and Sequences
- CodeForces 450-B. Jzzhu and Sequences
- Codeforces #257 div2 B. Jzzhu and Sequences
- CodeForces 450B Jzzhu and Sequences
- codeforces 450B Jzzhu and Sequences
- CodeForces-450B Jzzhu and Sequences
- codeforces 450B Jzzhu and Sequences
- CodeForces 450B Jzzhu and Sequences
- Codeforces 450B Jzzhu and Sequences
- CodeForces 450B-Jzzhu and Sequences
- “共享睡眠舱”被北京警方叫停,天津同一时间上线“升级版”
- 凭阿里和腾讯实力,怎么今年才进世界500强?
- 索赔 878 万,ofo 小黄车死亡事故后,当事人父母首开口
- 打开qtcreator出现提示无法覆盖文件 /home/xxx/.config/Qtcreator/qtversion.xml : Permission denied
- 光栅渲染器(三)光栅化2D三角形
- Jzzhu and Sequences CodeForces
- Spring注解的快速入门(02)
- 数据结构之 栈stack 模板类(数组描述方法)
- 酷炫,谷歌地图可以带你遨游宇宙空间站啦
- 专访小学宝创始人蒋晟:离开百度、搜狗,他想凭借 AI 撬动4500亿小学课外辅导市场
- 联想研究院八高管首次集体亮相 解析AI战略
- 业界 | 23篇论文入选CVPR2017, 商汤科技精选论文解读
- 市值超过 2900 亿的海康威视半年净利润 32.92 亿元,同比增长 26.22%
- 刚刚发布语音交互系统,猎户星空又刷新了人脸识别世界纪录