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的个数是三的倍 ...
随机推荐
- 第10章 同步设备I/O和异步设备I/O(2)_同步IO和异步IO基础
10.3 执行同步设备I/O (1)对设备读写操作的函数 ①ReadFile/WriteFile函数 参数 描述 hFile 文件句柄 pvBuffer 指向要接收文件数据的缓冲区或把缓冲区数据写入设 ...
- bzoj1082[SCOI2005]栅栏
Description 农夫约翰打算建立一个栅栏将他的牧场给围起来,因此他需要一些特定规格的木材.于是农夫约翰到木材店购 买木材.可是木材店老板说他这里只剩下少部分大规格的木板了.不过约翰可以购买这些 ...
- python-数据类型补充及文件处理操作
___数据类型____ 一.列表的复制 浅复制和深复制 浅复制只复制一层,深复制完全克隆,慎用 1.实现浅复制的三种方式: name=['song','xiao','nan'] import copy ...
- css实现省略号
样式: {width: 160px; overflow: hidden; text-overflow:ellipsis; white-space: nowrap;} 说明: white-space: ...
- (原创)mybatis学习一,夯实基础
一,what?(是什么) MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可 ...
- Jquery操作下拉框(DropDownList)实现取值赋值
Jquery操作下拉框(DropDownList)想必大家都有所接触吧,下面与大家分享下对DropDownList进行取值赋值的实现代码 1. 获取选中项: 获取选中项的Value值: $('sele ...
- 清北学堂2017NOIP冬令营入学测试P4749 C’s problem(c)
P4746 C's problem(c) 时间: 1000ms / 空间: 655360KiB / Java类名: Main 背景 冬令营入学测试 描述 题目描述 小C是一名数学家,由于它自制力比较差 ...
- maven常用插件: 打包源码 / 跳过测试 / 单独打包依赖项
一.指定编译文件的编码 maven-compile-plugin <plugin> <groupId>org.apache.maven.plugins</groupId& ...
- Theano2.1.6-基础知识之在thenao中的求导
来自:http://deeplearning.net/software/theano/tutorial/gradients.html Derivatives in Theano 一.计算梯度 现在,让 ...
- WinObjc - 使用iOS项目生成通用Windows应用
Github上一周年的WinObjc项目最近发布了预览版本,终于等到了这一天.WinObjc项目就是Build 2015大会上微软宣布的Project IslandWood项目,致力于将iOS应用快速 ...