Jzzhu and Sequences(CF-450B)
Problem Description
Jzzhu has invented a kind of sequences, they meet the following property:
You are given x and y, please calculate fn modulo 1000000007 (109 + 7).
Input
The first line contains two integers x and y (|x|, |y| ≤ 109). The second line contains a single integer n (1 ≤ n ≤ 2·109).
Output
Output a single integer representing fn modulo 1000000007 (109 + 7).
Examples
Input
2 3
3Output
1
Input
0 -1
2Output
1000000006
题意:输入 x,y,n,其中 f(1)=x,f(2)=y,f(i)=f(i-1)+f(i+1),求 f(n)
思路: n 很大,直接递推的话一定会 TLE
可以考虑构造满足递推式的矩阵用矩阵快速幂来求
已知:,那么有:
即:,则:
构造系数矩阵,有:
化简得:
所以答案即为系数矩阵的 n-2 次幂的值 A[1][1]*y+A[1][2]*x
此外要注意特判
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define PI acos(-1.0)
#define E 1e-9
#define INF 0x3f3f3f3f
#define LL long long
const int MOD=1e9+7;
const int N=10+5;
const int dx[]= {-1,1,0,0};
const int dy[]= {0,0,-1,1};
using namespace std;
struct Matrix{LL s[N][N];
};
Matrix e;//单位矩阵E
Matrix x;//构造矩阵
void init(){for(int i=1;i<=2;i++)//主对角线为1e.s[i][i]=1;//构造矩阵x.s[1][1]=1;x.s[1][2]=-1;x.s[2][1]=1;x.s[2][2]=0;
}
Matrix mul(Matrix A,Matrix B,LL n){//矩阵乘法,n代表A、B两个矩阵是n阶方阵Matrix temp;//临时矩阵,存放A*B结果for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)temp.s[i][j]=0;for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)for(int k=1;k<=n;k++)temp.s[i][j]=(temp.s[i][j]+A.s[i][k]*B.s[k][j])%MOD;return temp;
}
Matrix quickPower(Matrix a,LL b,LL n){//矩阵快速幂,求矩阵n阶矩阵的b次幂Matrix ans=e;while(b){if(b&1)ans=mul(ans,a,n);//ans=e*aa=mul(a,a,n);//a=a*ab>>=1;}return ans;
}
int main(){init();LL x0,y0,n;while(scanf("%lld%lld%lld",&x0,&y0,&n)!=EOF){if(x0==0&&y0==0)printf("0\n");else if(n==1)printf("%lld\n",(x0%MOD+MOD)%MOD);else if(n==2)printf("%lld\n",(y0%MOD+MOD)%MOD);else{Matrix res=quickPower(x,n-2,2);LL temp=(res.s[1][1]*y0+res.s[1][2]*x0)%MOD;if(temp<0)temp=(temp%MOD+MOD)%MOD;printf("%lld\n",temp);}}return 0;
}
Jzzhu and Sequences(CF-450B)相关推荐
- codeforces 900D. Unusual Sequences(莫比乌斯反演)
900D. Unusual Sequences(莫比乌斯反演) 题目链接:传送门 题意: 给出 xxx 和 yyy ,求序列形如 a1,a2..ana_1,a_2..a_na1,a2..an 满 ...
- 【解题报告】随便练练二(CF 2300)
[解题报告]随便练练二(CF 2300) A:Antimatter | CF383D 题意 思路 :DP 代码 B:Physical Education Lessons | CF915E 题意 思路一 ...
- Commentator problem(CF 2)
题目链接 题目大意: 给定三个圆,询问是否存在点满足该点与三个圆夹角均相等,若存在多组解返回夹角最大值. 圆外一点到两圆夹角均相等: 即 sina = sinb = r1 / d1 = r2 / d2 ...
- Two Merged Sequences(CF 1144 G)
前言 在做其它题的时候脑补到过这个题意,没想到还真有这样的题 题目相关 链接 题目大意 给一个序列,问是否能将这个序列完全划分成一个上升子序列和下降子序列 数据范围 n≤2⋅105n\le2·10^5 ...
- UPC 6617 Finite Encyclopedia of Integer Sequences(找规律)
题目描述 In Finite Encyclopedia of Integer Sequences (FEIS), all integer sequences of lengths between 1 ...
- 2018.09.23 codeforces 1053B. Vasya and Good Sequences(前缀和)
传送门 考试的时候卡了一会儿. 显然这个答案只跟二进制位为1的数量有关. 还有一个显然的结论. 对于一个区间[l,r][l,r][l,r],如果其中单个数二进制位为1的数量最大值不到区间所有数二进制位 ...
- D. Make a Power of Two(cf#739DIV3)
D. Make a Power of Two 链接: link. 题意: 找出将数字转换为 2 的任意幂的最小移动次数. 题解: 先将2的xxx次幂的结果以字符串形式保存,输入字符串nnn后,因为存在 ...
- Web of Lies(CF 1548A)
这是今天在打个人赛时碰见的一道题,是一道半图论半思维的题. Web of Lies 题目大意不难理解,在这里只需要注意一些细节.在加边时,只有当cnt[min]的值为1时答案才应该减1,而不是当cnt ...
- Magic Powder - 2 (CF 670_D)
http://codeforces.com/problemset/problem/670/D2 The term of this problem is the same as the previous ...
最新文章
- NFS 网络挂载问题 解决
- 有趣的js匿名函数写法(function嵌套)
- c#Ice开发之环境配置(一)
- linux系统升级后,手动编译的kernel无法启动问题
- 这是很简单的js拖拽方法
- 漫画:什么是堆排序?
- Vs2010 上的配置Opencv2.2
- 金蝶KIS 14.1 专业版安装教程
- 前端实战|React18项目启动——pc端极客园项目前置准备
- DSP,从入门到入土
- 【Android 逆向】Android 中常用的 so 动态库 ( libm.so 数学函数动态库 | liblog.so 日志模块动态库 | libselinux.so 安全模块动态库 )
- Dreamweaver CS6 完全自学教程 (一)
- ubuntu双系统时间不一致现象
- 京区航天研究所 哪些比较好的研究所?
- Java中的BigDecimal比较大于小于等于,四舍五入保留几位(setScale方法详解),加减乘除取余
- ug585-Zynq-7000中文文档阅读笔记
- Java 开发环境搭建
- ggplot2图形排版:patchwork包简单入门
- 全球及中国二手车市场销量渠道规模及发展格局建议报告2021-2027年
- 影响艾默生流量计测量误差的主要原因