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. 过河 2005年NOIP全国联赛提高组(离散化+dp)

    1105 过河 2005年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond       题目描述 Description 在河上有一 ...

  2. 342 Power of Four 4的幂

    给定一个整数 (32位有符整数型),请写出一个函数来检验它是否是4的幂.示例:当 num = 16 时 ,返回 true . 当 num = 5时,返回 false.问题进阶:你能不使用循环/递归来解 ...

  3. 笨拙而诡异的 Oracle(之二)

    有一张表,很多数据:   想取某个月的数据.初始的想法很简单,根据日期(RQ)形成条件即可:  符合条件的记录数是 129835,但耗时太长:14.515 秒(RQ字段是做过索引的)!直观的反应是 O ...

  4. DAO模式详解

    DAO模式 数据访问层(DAO): 数据的增.删.改.查操作: 业务逻辑层(service): 业务来往的操作,需要调用数据访问层则调用数据访问层,传递数据: 表现层(UI): 呈现数据,用户交互. ...

  5. 将电脑浏览器User-Agent识别改成手机浏览器UA几种简单方法

    第一种方法:修改浏览器的快捷方式 右击桌面上的Chrome浏览器图标,在弹出的右键菜单中选择“复制”,复制一个图标副本到桌面.右击该副本,选择“属性”,打开相应的对话框,在“目标”文本框的字符后面添加 ...

  6. MySQL实现当前数据表的所有时间都增加或减少指定的时间间隔

    DATE_ADD() 函数向日期添加指定的时间间隔. 当前表所有数据都往后增加一天时间: UPDATE ACT_BlockNum SET CreateTime = DATE_ADD(CreateTim ...

  7. java编码终极探秘

    首先要明白,java中string字符串都是unicode码保存的,只不过显示的时候会根据一定的规则,比如GBK或者是UTF-8去对照表中查找进行显示. 之所以会乱码就是因为使用错了编码方式. 数据是 ...

  8. easyui 使用笔记

    http://www.easyui.info/archives/1435.html datagrid 服务端分页 服务端分页,高效,快捷!强力推荐! easyui的datagrid服务端分页,通过设置 ...

  9. C# WebKitBrowser 设置内容

    WebKit.WebKitBrowser kitBrowser = new WebKit.WebKitBrowser(); kitBrowser.Dock = DockStyle.Fill; // k ...

  10. Linux 通过cksum 来判断文件是否是相同

    1. 最近scp部署文件时 发现日期会发生变化 (刚查了下 可以使用 -p 命令进行处理) 会变成部署时的日期. 不好判断文件倒是有没有部署 2. 最简单的办法 我mount了 补丁服务器  到lin ...