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的个数是三的倍 ...
随机推荐
- ComboBox的数据联动
实现效果: 点击年级下拉框值时,获取科目下拉框值 一:加载年级下拉框值 GradeDAL层: //检索所有年级名称集合,返回的是泛型集合List<Grade> public List< ...
- Code! MVC 5 App with Facebook, Twitter, LinkedIn and Google OAuth2 Sign-on (C#)
http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2- ...
- CSS语法
在CSS中,通过id选择元素和JavaScript一样:#idname.也可以在最前面加上元素名,防止id相同的冲突.
- java多线程系类:基础篇:06线程让步
本系类的知识点全部来源于http://www.cnblogs.com/skywang12345/p/3479243.html,我只是复制粘贴一下,特在此说明. 概要 本章,会对Thread中的线程让步 ...
- SQL 按特定字段值排序
SQL 按特定字段值排序的代码,有需要的朋友可以参考下. id, name shandong01 name1 shandong02 name2 shandong03 name3 beijing01 n ...
- Linux共享库 socket辅助方法
//sockhelp.h#ifndef _vx #define _vx #ifdef __cplusplus extern "C" { #endif /** * readn - 读 ...
- JavaScript弹出窗口技巧
window.open ('faces/newPage.jsp','newwindow','height=300,width=700, top=50,left=50,toolbar=no,menuba ...
- struts2: 通过流输出实现exce导出
参考下面代码,在Action中加一个方法: // 导出excel public String excel() throws Exception { StringBuffer excelBuf = ne ...
- java: ant 脚本示例
<?xml version="1.0" encoding="UTF-8"?> <!--basedir是从build.xml所在的目录为基础算起 ...
- Unity 5.3.1 No Android/IOS module loaded
unity我一直在用5.0以下的版本 昨天升级到了最新版本5.3.1 发现无法打android包,ios也不行 提示“No Android/IOS module loaded” 下面有个Module ...