数学 找规律 Jzzhu and Sequences
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
2 3
3
1
0 -1
2
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).
第一次用矩阵快速幂 做不出来 可能是因为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的更多相关文章
- # E. Mahmoud and Ehab and the xor-MST dp/数学+找规律+xor
E. Mahmoud and Ehab and the xor-MST dp/数学/找规律 题意 给出一个完全图的阶数n(1e18),点由0---n-1编号,边的权则为编号间的异或,问最小生成树是多少 ...
- Harmonic Number (II) 数学找规律
I was trying to solve problem '1234 - Harmonic Number', I wrote the following code long long H( int ...
- 数学/找规律/暴力 Codeforces Round #306 (Div. 2) C. Divisibility by Eight
题目传送门 /* 数学/暴力:只要一个数的最后三位能被8整除,那么它就是答案:用到sprintf把数字转移成字符读入 */ #include <cstdio> #include <a ...
- 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 ...
- HDU 5914 Triangle 数学找规律
Triangle 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5914 Description Mr. Frog has n sticks, who ...
- ural 2029 Towers of Hanoi Strike Back (数学找规律)
ural 2029 Towers of Hanoi Strike Back 链接:http://acm.timus.ru/problem.aspx?space=1&num=2029 题意:汉诺 ...
- HDU 1273 漫步森林(数学 找规律)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1273 漫步森林 Time Limit: 2000/1000 MS (Java/Others) M ...
- SGU 105 数学找规律
观察一下序列,每3个数一组,第一个数余1,不能,加第二个数后整除(第二个数本身余2),第三数恰整除.一行代码的事.011011011.... #include<iostream> usin ...
- hdu5967数学找规律+逆元
Detachment Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
随机推荐
- 简单理解jsonp原理
对于javascript程序员来说,发送ajax请求获取后台数据然后把数据和模板拼接成字符串渲染回DOM实现无刷新更新页面这样的操作可谓是轻车熟路.但众所周知,ajax有一个不好,就是不能跨域传输数据 ...
- [Swift通天遁地]四、网络和线程-(7)检测服务器接口的访问状态:验证请求结果和可访问性
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- [Swift通天遁地]八、媒体与动画-(13)CoreText框架实现图文混排
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- composer 下载安装
linux/mac os curl -sS https://getcomposer.org/installer | php mv composer.phar /usr/local/bin/compos ...
- IOS上微信在输入框弹出键盘后,页面不恢复,下方有留白,有弹窗弹出时页面内容感应区域错位
问题说明: ios中,键盘的弹起,页面会往上挪动,使输入框展示在页面中间,键盘隐藏页面会下挪恢复原状. 在微信移动端,ios页面不恢复,下方有留白. 收起键盘的瞬间,如果有弹窗弹出,此时时页面内容应区 ...
- Testng1
Testng 简介: Testng是一套开源测试框架,是从Junit继承而来,testng意为test next generation,主要有以下特性: annotations 注释,如 @test ...
- JavaScript入门三
*********BOM和DOM******** JavaScript分为 ECMAScript,DOM,BOM. BOM(Browser Object Model)是指浏览器对象模型,它使 Java ...
- Codeforces 792D
题意:给定一棵拥有n个节点的满二叉树(即n==2^x-1),q个查询,每次给出一个节点的编号,再给出一个由L,R,U组成的字符串序列,依次表示向左子节点.右子节点.父节点移动,如果移动不合法,则忽略. ...
- [转]linux uniq 命令详解
转自:http://blog.csdn.net/tianmohust/article/details/6997683 uniq 命令 文字 uniq 是LINUX命令 用途 报告或删除文件中重复的 ...
- centos安装composer以及使用国内镜像
下载composer.phar文件 curl -sS https://getcomposer.org/installer | php 将composer.phar移动到环境变量中并且更名为compos ...