题目链接:http://codeforces.com/problemset/problem/450/B


B. Jzzhu and Sequences
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 test(s)
input
2 3
3
output
1
input
0 -1
2
output
1000000006
Note

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 <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
struct A
{
int mat[2][2];
};
A d,f;
__int64 n,mod;
A mul(A a,A b)
{
A t;
memset(t.mat,0,sizeof(t.mat));
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n;j++)
{
// if(a.mat[i][k])
for(int k = 0; k < n; k++)
{
t.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
t.mat[i][j]%=mod;
}
}
}
return t;
}
A quickP(int k)
{
A p = d ,m;
memset(m.mat,0,sizeof(m.mat));
for(int i=0;i<n;++i)//单位矩阵
{
m.mat[i][i]=1;
}
while(k)
{
if(k & 1)
m=mul(m,p);
p=mul(p,p);
k >>= 1 ;
}
return m;
}
int main()
{
n=2;
int k,t;__int64 x,y,z;
while(scanf("%I64d%I64d",&x,&y)!=EOF)
{
int s=0;
scanf("%I64d",&z);
mod=1000000007;
if(z == 1)
{
if(x < 0)
printf("%I64d\n",x+mod);
else
printf("%I64d\n",x);
continue;
}
d.mat[0][1]=-1;d.mat[1][1] = 0;
d.mat[0][0]=d.mat[1][0]=1;
A ret=quickP(z-2);//z-2 乘的次数
__int64 ans=(ret.mat[0][0]*y%mod+ret.mat[0][1]*x%mod)%mod;
if(ans < 0)
ans+=mod;
printf("%I64d\n",ans);
}
return 0;
}

Codeforces Round #257(Div. 2) B. Jzzhu and Sequences(矩阵高速幂)的更多相关文章

  1. Codeforces Round #257 (Div. 2) B. Jzzhu and Sequences (矩阵快速幂)

    题目链接:http://codeforces.com/problemset/problem/450/B 题意很好懂,矩阵快速幂模版题. /* | 1, -1 | | fn | | 1, 0 | | f ...

  2. Codeforces Round #257 (Div. 2) B Jzzhu and Sequences

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

  3. Codeforces Round #257 (Div. 2/B)/Codeforces450B_Jzzhu and Sequences

    B解题报告 算是规律题吧,,,x y z -x -y -z 注意的是假设数是小于0,要先对负数求模再加模再求模,不能直接加mod,可能还是负数 给我的戳代码跪了,,. #include <ios ...

  4. Codeforces Round #257 (Div. 1)449A - Jzzhu and Chocolate(贪婪、数学)

    主题链接:http://codeforces.com/problemset/problem/449/A ------------------------------------------------ ...

  5. Codeforces Round #257 (Div. 2) A. Jzzhu and Children(简单题)

    题目链接:http://codeforces.com/problemset/problem/450/A ------------------------------------------------ ...

  6. Codeforces Round #257 (Div. 1) C. Jzzhu and Apples (素数筛)

    题目链接:http://codeforces.com/problemset/problem/449/C 给你n个数,从1到n.然后从这些数中挑选出不互质的数对最多有多少对. 先是素数筛,显然2的倍数的 ...

  7. Codeforces Round #257 (Div. 1) D - Jzzhu and Numbers 容斥原理 + SOS dp

    D - Jzzhu and Numbers 这个容斥没想出来... 我好菜啊.. f[ S ] 表示若干个数 & 的值 & S == S得 方案数, 然后用这个去容斥. 求f[ S ] ...

  8. Codeforces Round #257 (Div. 2) C. Jzzhu and Chocolate

    C. Jzzhu and Chocolate time limit per test 1 second memory limit per test 256 megabytes input standa ...

  9. Codeforces Round #257 (Div. 2) A. Jzzhu and Children

    A. Jzzhu and Children time limit per test 1 second memory limit per test 256 megabytes input standar ...

随机推荐

  1. 0x58B 四边形不等式

    bzoj1563: [NOI2009]诗人小G 还有优化二维区间DP的,形如f[i][j]min{f[i][k]+f[k][j+1]+val(i,j)} 其中val满足四边形不等式,而且对于任意a&l ...

  2. Node.js:常用工具

    ylbtech-Node.js:常用工具 1.返回顶部 1. Node.js 常用工具 util 是一个Node.js 核心模块,提供常用函数的集合,用于弥补核心JavaScript 的功能 过于精简 ...

  3. 2014.9.20Hashtable概述

    hashtable叫哈希表,用于表示键值的集合,这些键值对根据键的哈希代码进行组织,其每个元素都存储于DictionaryEntry对象中的键值对.键不能为空引用. count:获取包含在hashta ...

  4. [Javascript] 40个轻量级JavaScript脚本库

    诸如jQuery, MooTools, Prototype, Dojo和YUI等JavaScript脚本库,大家都已经很熟悉.但这些脚本库有利也有弊--比如说JavaScript文件过大的问题.有时你 ...

  5. 兼容各个浏览器的jquyer zclip复制文本插件 无效的解决办法

    项目中使用点击文本复制功能,用了这个兼容各个浏览器的插件,但是发现放在最前面正常,放到嵌套的html中就失效. 解决办法: <span style="position: relativ ...

  6. USB接口大全

    USB2.0系列: Standard A: Standard B: Mini Standard B: Micro USB: ** Micro USB 与 Mini B比较 ** USB3.0系列: U ...

  7. 用css修改HTML5 input placeholder颜色

    使用CSS修改HTML5 input placeholder颜色 本文选自StackOverflow(简称:SOF)精选问答汇总系列文章之一,本系列文章将为读者分享国外最优质的精彩问与答,供读者学习和 ...

  8. WIN系统查询版本

    cmd -> DISM /online /Get-CurrentEdition //查询系统版本 WIN+R -> slmgr.vbs -ipk 查询系统注册信息slmgr.vbs -dl ...

  9. CDR案例:广告条幅banner设计

    本教程练习使用裁剪.位图.变换.阴影.透明度等特殊效果等工具制作广告条幅banner,具体操作如下. 1. 执行“文件”→“新建”命令,打开“创建新文档”对话框,在“宽度”选框右侧选择单位为“像素”, ...

  10. hibernate一对多和多对一配置

    public class Dept { private int deptId; private String deptName; // [一对多] 部门对应的多个员工 private Set<E ...