Codeforces450 B. Jzzhu and Sequences
1 second
256 megabytes
standard input
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).
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 a single integer representing fn modulo 1000000007 (109 + 7).
2 3
3
1
0 -1
2
1000000006
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 <cstring>
using namespace std;
/*矩阵快速幂*/
#define mod 1000000007
#define ll long long int
#define sec 2
struct mat
{
ll arr[sec][sec];
};
mat mul(mat a, mat b)
{
mat res;
memset(res.arr, , sizeof(res.arr));
for (int i = ; i < sec; i++)
for (int j = ; j < sec; j++)
for (int k = ; k < sec; k++)
{
res.arr[i][j] = ((res.arr[i][j] + a.arr[i][k] * b.arr[k][j]) % mod + mod) % mod;
}
return res;
}
mat expo(mat ori, int n)
{
mat res;
memset(res.arr, , sizeof(res.arr));
for (int i = ; i < sec; i++)
res.arr[i][i] = ;
while (n)
{
if (n & )
res = mul(res, ori);
ori = mul(ori, ori);
n >>= ;
}
return res;
}
int main()
{
int x, y, n;
while (cin >> x >> y >> n)
{
x = (x + mod) % mod;
y = (y + mod) % mod;
if (n == )
cout << x << endl;
else
if (n == )
cout << y << endl;
else
{
mat a;
a.arr[][] = a.arr[][] = ;
a.arr[][] = -;
a.arr[][] = ;
a = expo(a, n - );
cout << ((a.arr[][] * y) % mod + (a.arr[][] * x) % mod) % mod << endl;
}
}
return ;
}
Codeforces450 B. Jzzhu and Sequences的更多相关文章
- Codeforces450 B. Jzzhu and Sequences (找规律)
题目链接:https://vjudge.net/problem/CodeForces-450B Jzzhu has invented a kind of sequences, they meet th ...
- CodeForces 450B Jzzhu and Sequences (矩阵优化)
CodeForces 450B Jzzhu and Sequences (矩阵优化) Description Jzzhu has invented a kind of sequences, they ...
- Codeforces Round #257 (Div. 2 ) B. Jzzhu and Sequences
B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...
- CodeForces - 450B Jzzhu and Sequences —— 斐波那契数、矩阵快速幂
题目链接:https://vjudge.net/problem/CodeForces-450B B. Jzzhu and Sequences time limit per test 1 second ...
- codeforces 450B B. Jzzhu and Sequences(矩阵快速幂)
题目链接: B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input ...
- CF450B Jzzhu and Sequences(矩阵加速)
CF450B Jzzhu and Sequences 大佬留言:这.这.不就是矩乘的模板吗,切掉它!! You are given xx and yy , please calculate $f_{n ...
- 数学 找规律 Jzzhu and Sequences
A - Jzzhu and Sequences Jzzhu has invented a kind of sequences, they meet the following property: ...
- Codeforces Round #257(Div. 2) B. Jzzhu and Sequences(矩阵高速幂)
题目链接:http://codeforces.com/problemset/problem/450/B B. Jzzhu and Sequences time limit per test 1 sec ...
- 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 ...
随机推荐
- bzoj 1913: [Apio2010]signaling 信号覆盖【旋转卡壳(?)】
参考:https://blog.csdn.net/qpswwww/article/details/45334033 讲的很清楚 做法比较像旋转卡壳但是具体是不是我也不清楚.. 首先知道只要求出每种方案 ...
- 洛谷 P3708 koishi的数学题
找规律发现\( f[i]=f[i-1]+n-\sum_{i的因数和} \) 一A了深(sh)蓝(ui)题的我被找规律绿题卡死 记得开long long #include<iostream> ...
- Ocelot(六)- 架构图
简介 Ocelot是一个用.NET Core实现并且开源的API网关,它功能强大,包括了:路由.请求聚合.服务发现.认证.鉴权.限流熔断.并内置了负载均衡器与Service Fabric.Butter ...
- python 关闭redis的连接
在python语言中使用redis时,没有找到对应的关闭的方法 try: self.redisconn = StrictRedisCluster(startup_nodes=self.redisNod ...
- bzoj1572 [Usaco2009 Open]工作安排Job【贪心 堆】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1572 尽管这一题没有看题解,但是耗时还是比本应耗费的时间要长,所以还是写一下,以提升经验 这 ...
- Bryce1010的linux课程设计
1.设计目的 2.软件环境 3.要求 4.需求分析 5.总体设计 6.详细设计 7.调试与测试 8.总结 思路整理: 1.如果要开始编译着手的准备 SQLite数据库的安装 gtk+的安装 (.... ...
- 题解报告:Luogu P3368 【模板】树状数组 2(区间修改,单点查询)
题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数数加上x 2.求出某一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别表示该数列数字的个数和操作的总个数. ...
- Optimizing Downloads for Efficient Network Access
Optimizing Downloads for Efficient Network Access Previous Next 1.This lesson teaches you to Unders ...
- java数组实现买彩票(重复则重新遍历查询思想)
package com.wh.shuzu; import java.util.Arrays; /** * 买彩票 * @author 丁璐同学 * 重复则重新遍历查询思想 */ public clas ...
- [转]ASP.net MVC 2 自定义模板来显示数据
本文转自:http://blog.163.com/liaojunbo@126/blog/static/1394892972012113104653651/ 在ASP.net MVC 2中,一个很有意思 ...