A - Jzzhu and Sequences

Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Appoint description: 
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

Input
2 3
3
Output
1
Input
0 -1
2
Output
1000000006

Hint

In the first sample, f2 = f1 + f3, 3 = 2 + f3f3 = 1.

In the second sample, f2 =  - 1;  - 1 modulo (109 + 7) equals (109 + 6).

 
观察他的公式f[1] = x,f[2] = y,f[i] = f[i] - f[i-1];
 
可以得到     |    -1     1   |  ^((n%2 ? n:(--n))/2)       ×  |    f[1]   |     =      |  f[n]     | 
                  |    -1    0   |                                             |    f[2]  |             |  f[n+1]  |
这样只要处理矩阵的次方后就能得出答案。
#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 矩阵的更多相关文章

  1. CodeForces 450B (矩阵快速幂模板题+负数取模)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=51919 题目大意:斐波那契数列推导.给定前f1,f2,推出指定第N ...

  2. CodeForces 450B Jzzhu and Sequences (矩阵优化)

    CodeForces 450B Jzzhu and Sequences (矩阵优化) Description Jzzhu has invented a kind of sequences, they ...

  3. CodeForces 450B Jzzhu and Sequences(矩阵快速幂)题解

    思路: 之前那篇完全没想清楚,给删了,下午一上班突然想明白了. 讲一下这道题的大概思路,应该就明白矩阵快速幂是怎么回事了. 我们首先可以推导出 学过矩阵的都应该看得懂,我们把它简写成T*A(n-1)= ...

  4. 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 ...

  5. CodeForces - 450B Jzzhu and Sequences —— 斐波那契数、矩阵快速幂

    题目链接:https://vjudge.net/problem/CodeForces-450B B. Jzzhu and Sequences time limit per test 1 second ...

  6. codeforces 450B B. Jzzhu and Sequences(矩阵快速幂)

    题目链接: B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input ...

  7. CodeForces 450B Jzzhu and Sequences 【矩阵快速幂】

    Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, ple ...

  8. codeforces 691E 矩阵快速幂+dp

    传送门:https://codeforces.com/contest/691/problem/E 题意:给定长度为n的序列,从序列中选择k个数(可以重复选择),使得得到的排列满足xi与xi+1异或的二 ...

  9. Xor-sequences CodeForces - 691E || 矩阵快速幂

    Xor-sequences CodeForces - 691E 题意:在有n个数的数列中选k个数(可以重复选,可以不按顺序)形成一个数列,使得任意相邻两个数异或的结果转换成二进制后其中1的个数是三的倍 ...

随机推荐

  1. Json数据的学习

    JSON 教程 <body><h2>JSON Object Creation in JavaScript</h2> <p>Name: <span ...

  2. Android之监听手机软键盘弹起与关闭

    背景: 在很多App开发过程中需要在Activity中监听Android设备的软键盘弹起与关闭,但是Android似乎没有提供相关的的监听API给我们来调用,本文提供了一个可行的办法来监听软键盘的弹起 ...

  3. Android终端配置isatap隧道使用IPV6的方法

    使用isatap隧道可以在手机有IPV4网络的情况下访问IPv6网络资源.关于isatap隧道的配置方法,清华.上交两所学校都给出了相应的在windows xp/2003,windows 2000,以 ...

  4. HTML 学习笔记 CSS样式(背景)

    背景色 可以使用background-color属性为元素设置背景色 这个属性接受任何合法的颜色值. 可以使用background-image属性来喂元素设置背景图片. 背景重复 如果需要在页面上对背 ...

  5. js物理弹性窗口

    js物理弹性窗口 点击下载代码

  6. java script 确认框

    <input name="Submit" type="submit" class="inputedit" value="删除 ...

  7. KeyBord事件从Activtiy层往下分发详细过程代码示例

    step1:调用Activity成员函数dispatchKeyEvent public boolean dispatchKeyEvent(KeyEvent event) { // Let action ...

  8. C#.NET 大型通用信息化系统集成快速开发平台 4.0 版本 - 组织机构的名称编号是否允许重复?

    通常情况下,一个公司内部的部门名称,编号是不可能重复的.但是是在多公司的情况下,很可能有部门名称重复的问题存在,这时需要允许部门名称重复. 例如一个大型IT公司,在2个地区都有研发部或者客户服务部,这 ...

  9. Cocos2d-x 3.4在AndroidStudio上编译配置

    转载请标明出处:http://www.cnblogs.com/studweijun/p/4320778.html 1.准备好以下文件 1) AndroidStudio:  https://dl.goo ...

  10. PHP 基础笔记

    数据类型 字符串 整数 浮点数 布尔值 数组 对象 NULL 未定义的变量,数据类型为 NULL. PHP 中数组和对象是不同的类型,而 js 中数组即为对象.(ps: es6 已经内置了 class ...