CodeForces 450B 矩阵
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
System Crawler (2016-04-23)
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).
Sample Input
2 3
3
1
0 -1
2
1000000006
Hint
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<map>
#include<set>
#include<string>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<cstdio>
#include<time.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1000000001
#define ll long long
#define MOD 1000000007
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int MAXN = ;
int x,y;
ll n;
struct Mat
{
ll a[][];
};
Mat operator *(Mat a,Mat b)
{
Mat c;
memset(c.a,,sizeof(c.a));
for(int i = ; i < ; i++){
for(int j = ; j < ; j++){
for(int k = ; k < ;k++){
c.a[i][j] += ((a.a[i][k] * b.a[k][j]) % MOD + MOD) % MOD;
c.a[i][j] = ((c.a[i][j])% MOD + MOD) % MOD;
}
}
}
return c;
}
Mat mod_pow(Mat b,int n)
{
Mat c;
c.a[][] = c.a[][] = ;
c.a[][] = c.a[][] = ;
while(n){
if(n & ){
c = c * b;
}
b = b * b;
n >>= ;
}
return c;
}
int main()
{
while(~scanf("%d%d%lld",&x,&y,&n)){
if(n == ){
x = (x%MOD + MOD)%MOD;
cout<<x<<endl;
continue;
}
else if(n == ){
y = (y%MOD + MOD)%MOD;
cout<<y<<endl;
continue;
}
else {
Mat b;
b.a[][] = -;
b.a[][] = ;
b.a[][] = -;
b.a[][] = ;
int t = n;
if(t % == )t --;
b = mod_pow(b,t/);
ll ans;
if(n % ){
ans = ((b.a[][] * x)%MOD + (b.a[][] * y)%MOD + MOD)%MOD;
}
else {
ans = ((b.a[][] * x)%MOD + (b.a[][] * y)%MOD + MOD)%MOD;
}
cout<<(ans + MOD) % MOD<<endl;
}
}
return ;
}
CodeForces 450B 矩阵的更多相关文章
- CodeForces 450B (矩阵快速幂模板题+负数取模)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=51919 题目大意:斐波那契数列推导.给定前f1,f2,推出指定第N ...
- CodeForces 450B Jzzhu and Sequences (矩阵优化)
CodeForces 450B Jzzhu and Sequences (矩阵优化) Description Jzzhu has invented a kind of sequences, they ...
- CodeForces 450B Jzzhu and Sequences(矩阵快速幂)题解
思路: 之前那篇完全没想清楚,给删了,下午一上班突然想明白了. 讲一下这道题的大概思路,应该就明白矩阵快速幂是怎么回事了. 我们首先可以推导出 学过矩阵的都应该看得懂,我们把它简写成T*A(n-1)= ...
- Codeforces 450B div.2 Jzzhu and Sequences 矩阵快速幂or规律
Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, ple ...
- CodeForces - 450B Jzzhu and Sequences —— 斐波那契数、矩阵快速幂
题目链接:https://vjudge.net/problem/CodeForces-450B B. Jzzhu and Sequences time limit per test 1 second ...
- codeforces 450B B. Jzzhu and Sequences(矩阵快速幂)
题目链接: B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input ...
- CodeForces 450B Jzzhu and Sequences 【矩阵快速幂】
Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, ple ...
- codeforces 691E 矩阵快速幂+dp
传送门:https://codeforces.com/contest/691/problem/E 题意:给定长度为n的序列,从序列中选择k个数(可以重复选择),使得得到的排列满足xi与xi+1异或的二 ...
- Xor-sequences CodeForces - 691E || 矩阵快速幂
Xor-sequences CodeForces - 691E 题意:在有n个数的数列中选k个数(可以重复选,可以不按顺序)形成一个数列,使得任意相邻两个数异或的结果转换成二进制后其中1的个数是三的倍 ...
随机推荐
- MySQL 5.7.x 配置教程
软件环境 操作系统:windows 10 x64 企业版 MySQL:mysql-5.7.11-winx64 MySQL官网下载:http://downloads.mysql.com/archives ...
- Unity StartCoroutine 和 yield return 深入研究
http://www.cnblogs.com/fly-100/p/3910515.html
- Flex(flash)检测摄像头的3种状态(是否被占用,没安装摄像头,正常)
在视频程序的编写过程中,我们经常要使用摄像头,在使用摄像头前有必要对摄像头的现有状态做个检测: 1.被占用 2.没安装摄像头 3.正常 camera=Camera.getCamera(); ...
- 由于启动用户实例的进程时出错,导致无法生成 SQL Server 的用户实例。该连接将关闭。
错误:由于启动用户实例的进程时出错,导致无法生成SQL Server的用户实例. 原因:添加安装SQLEXPRESS时,估计装在了不同的目录下: 解决方法:关闭Sqlserver及相关的程序,删除目录 ...
- [转]关于vs调试正确。但是发布到iis就出现无法访问后天局面
最近使用extjs+ashx进行ajax请求过程操作的时候发现一个问题..当我把程序发布到iis的时候就出现一只不执行到success回调函数. 当弹出状态值一看尽然是500.我就纳闷了.又没有语法错 ...
- Linux下修改系统编码的操作记录
Linux系统安装后,发现中文显示乱码.因为系统编码为en_US.UTF-8,应改为支持中文的编码(即zh_CN.UTF-8)操作记录如下:1)检查linux的系统编码检查linux的系统编码,确定系 ...
- apt-get update更新源时,出现“Hash Sum mismatch”问题
当使用apt-get update更新源时,出现下面"Hash Sum mismatch"的报错,具体如下:root@localhost:~# apt-get update.... ...
- 用Javascript判断访问来源操作系统, 设备, 浏览器类型
var browser = { os : function() { var u = navigator.userAgent; return {// 操作系统 linux: !!u.match(/\(X ...
- Java集合系列:-----------07Map架构
我们之前了解了Collection框架,我们再来了解一下Map架构.前面我们讲了Conllection中的List结合,没有讲Collection中的Set集合, 因为Collection框架中的Se ...
- 错题724-java
1.代码片段: byte b1=1,b2=2,b3,b6; final byte b4=4,b5=6; b6=b4+b5; b3=(b1+b2); System.out.println(b3+b6); ...