A - Jzzhu and Sequences

 

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

Example

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

Note

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

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

第一次用矩阵快速幂 做不出来 可能是因为N太大了

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 50000
#define N 21
#define MOD 1000000007
#define INF 1000000009
const double eps = 1e-;
const double PI = acos(-1.0); //斐波那契数列求第N项
struct Mat
{
LL data[][];
Mat(LL d1, LL d2, LL d3, LL d4)
{
data[][] = d1, data[][] = d2, data[][] = d3, data[][] = d4;
}
Mat operator*(const Mat& rhs)
{
Mat result(,,,);
result.data[][] = (data[][] * rhs.data[][] + data[][] * rhs.data[][]+ MOD )%MOD;
result.data[][] = (data[][] * rhs.data[][] + data[][] * rhs.data[][]+ MOD )%MOD;
result.data[][] = (data[][] * rhs.data[][] + data[][] * rhs.data[][]+ MOD )%MOD;
result.data[][] = (data[][] * rhs.data[][] + data[][] * rhs.data[][]+ MOD )%MOD;
return result;
}
};
void Print(const Mat& tmp)
{
cout << tmp.data[][] << ' ' << tmp.data[][] << endl;
cout << tmp.data[][] << ' ' << tmp.data[][] << endl << endl;
}
Mat fpow(Mat a, LL b)
{
Mat tmp = a, ret(,,,);
while (b != )
{
//Print(ret);
if (b & )
ret = tmp*ret;
tmp = tmp*tmp;
b /= ;
}
//Print(ret);
return ret;
}
int main()
{
LL x, y, n;
while (cin >> x >> y >> n)
{
if (n == )
cout << (x + MOD) % MOD << endl;
else if (n == )
cout << (y + MOD) % MOD << endl;
else
{
Mat m = fpow(Mat(, -, , ), n - );
cout << (m.data[][] * y + m.data[][] * x + MOD) % MOD << endl;
}
}
}

其实一个滚动数组即可解决

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 50000
#define N 21
#define MOD 1000000007
#define INF 1000000009
const double eps = 1e-;
const double PI = acos(-1.0); LL a[N];
int main()
{
LL x, y, n;
while (cin >> x >> y >> n)
{
a[] = (x + MOD) % MOD;
a[] = (y + MOD) % MOD;
for (int i = ; i < ; i++)
a[i] = (a[i - ] - a[i - ] + MOD) % MOD;
cout << (a[(n - ) % ] + MOD) % MOD << endl;
}
}

数学 找规律 Jzzhu and Sequences的更多相关文章

  1. # E. Mahmoud and Ehab and the xor-MST dp/数学+找规律+xor

    E. Mahmoud and Ehab and the xor-MST dp/数学/找规律 题意 给出一个完全图的阶数n(1e18),点由0---n-1编号,边的权则为编号间的异或,问最小生成树是多少 ...

  2. Harmonic Number (II) 数学找规律

    I was trying to solve problem '1234 - Harmonic Number', I wrote the following code long long H( int  ...

  3. 数学/找规律/暴力 Codeforces Round #306 (Div. 2) C. Divisibility by Eight

    题目传送门 /* 数学/暴力:只要一个数的最后三位能被8整除,那么它就是答案:用到sprintf把数字转移成字符读入 */ #include <cstdio> #include <a ...

  4. UVA 12683 Odd and Even Zeroes(数学—找规律)

    Time Limit: 1000 MS In mathematics, the factorial of a positive integer number n is written as n! an ...

  5. HDU 5914 Triangle 数学找规律

    Triangle 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5914 Description Mr. Frog has n sticks, who ...

  6. ural 2029 Towers of Hanoi Strike Back (数学找规律)

    ural 2029 Towers of Hanoi Strike Back 链接:http://acm.timus.ru/problem.aspx?space=1&num=2029 题意:汉诺 ...

  7. HDU 1273 漫步森林(数学 找规律)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1273 漫步森林 Time Limit: 2000/1000 MS (Java/Others)    M ...

  8. SGU 105 数学找规律

    观察一下序列,每3个数一组,第一个数余1,不能,加第二个数后整除(第二个数本身余2),第三数恰整除.一行代码的事.011011011.... #include<iostream> usin ...

  9. hdu5967数学找规律+逆元

    Detachment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

随机推荐

  1. PCB genesis孔符制作实现方法

    一.先看genesis原始孔符 孔符的作用:用于表示孔径的大小的一种代号, 当孔径检测时,可以按分孔图中的孔符对应的孔径尺寸对孔径检测. 在实际PCB行业通常不使用原始(图形)孔符,而使用字母孔符(如 ...

  2. Linux基本命令 文件管理 下部

    1.1 移动文件 将/data目录移动到/root下 涉及命令mv [root@oldboyedu-50 ~]# mv /data/ /root/ 移动 [root@oldboyedu-50 ~]# ...

  3. E20170930-hm

    parse   vt. 从语法上描述或分析(词句等);

  4. 14款形态各异的超时尚HTML5时钟动画

    14款超时尚的HTML5时钟动画(附源码)   时钟动画在网页应用中也非常广泛,在一些个人博客中,我们经常会看到一些相当个性化的HTML5时钟动画.今天我们向大家分享了14款形态各异的超时尚HTML5 ...

  5. 51nod 1222 莫比乌斯反演

    思路: yhx找的反演题 题解已经烂大街了 #pragma GCC optimize("O3") //By SiriusRen #include <bits/stdc++.h ...

  6. PHP开发之旅-验证码功能实现

    验证码这样的功能可以说是无处不在了,接下来使用php来实现验证码这样的功能,这里我是将验证码实现抽取到一个类中独立开来,那么后面如果再使用到验证码功能,直接引入该类文件并创建该类的实例,就可以使用验证 ...

  7. STL之vector篇

    #include<iostream> #include<cstdio> #include<cstring> #include<vector> #incl ...

  8. Resources.getResourceAsReader 报空指针

    1.maven项目中 可能未编译 导致找不到, 解决方法:mvn clean install -DskipTests -X   编译一下项目 2.可能在 Resources.getResourceAs ...

  9. 8、scala面向对象编程之对象

    1.  Object 2.伴生对象 3.让object继承抽象类 4.apply方法 5.main方法 6.用object实现枚举功能 1.  Object Object,相当于class的单个实例, ...

  10. 论文deadline 最后三天

    2015.12.29 星期二 内容整改 2015.12.30 星期三 参考文献,摘要等 2015.12.31 星期四 最后修改 尽最大的努力去做好论文的事情.