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. 02、监听key是否正确

    SDKInitializer其实是有两个action,第三个SDK_BROADTCAST_INTENT_EXTRA_INFO_KEY_ERROR_CODE虽然是一个常量但是它不是action.第二个a ...

  2. Android开发中常用的一些小技巧(转载)

    http://www.jb51.net/article/61135.htm Activity.startActivities() 常用于在应用程序中间启动其他的Activity. TextUtils. ...

  3. 个人微信二次开发API接口

    通过这个API接口可以做什么? 通过我们提供的API接口您可以开发: 工作手机(如:X创,X码,XX管家等) 微信群讲课软件(如:讲课X师,一起X堂等) 微信社群管理软件(如:小X管家,微X助手等) ...

  4. Prime Ring Problem -- 第一个真正意义上的 搜索

    这个搜索............搜的我头都大了.......不过还是 懂了那么一点点...哈哈 从3/7晚上  做到3/8晚上------从女生到妇女  我都用来做着一道题了......... 所谓的 ...

  5. JavaScript--如何插入JS

    我们来看看如何写入JS代码?你只需一步操作,使用<script>标签在HTML网页中插入JavaScript代码.注意, <script>标签要成对出现,并把JavaScrip ...

  6. 题解报告:hdu 3790 最短路径问题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3790 Problem Description 给你n个点,m条无向边,每条边都有长度d和花费p,给你起 ...

  7. 批量建文件夹【win7企业版】

    在记事本中notepad.exe按如下格式编辑好 md + 空格 + 文件夹名字(多个之间用空格隔开) 保存为.bat格式,运行即可批量创建文件夹.

  8. SAS进阶《深入解析SAS》之Base SAS基础、读取外部数据到SAS数据集

    SAS进阶<深入解析SAS>之Base SAS基础.读取外部数据到SAS数据集 前言:在学习完<SAS编程与商业案例>后,虽然能够接手公司的基本工作,但是为了更深入的SAS学习 ...

  9. html——导航demo

    通过行内块.伪类对导航栏进行设置 <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...

  10. codeforces_731C_[dfs][并查集]

    C. Socks time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...