Codeforces Round #257(Div. 2) B. Jzzhu and Sequences(矩阵高速幂)
题目链接:http://codeforces.com/problemset/problem/450/B
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 <cstdio>
#include <cstring>
using namespace std;
struct A
{
int mat[2][2];
};
A d,f;
__int64 n,mod;
A mul(A a,A b)
{
A t;
memset(t.mat,0,sizeof(t.mat));
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n;j++)
{
// if(a.mat[i][k])
for(int k = 0; k < n; k++)
{
t.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
t.mat[i][j]%=mod;
}
}
}
return t;
}
A quickP(int k)
{
A p = d ,m;
memset(m.mat,0,sizeof(m.mat));
for(int i=0;i<n;++i)//单位矩阵
{
m.mat[i][i]=1;
}
while(k)
{
if(k & 1)
m=mul(m,p);
p=mul(p,p);
k >>= 1 ;
}
return m;
}
int main()
{
n=2;
int k,t;__int64 x,y,z;
while(scanf("%I64d%I64d",&x,&y)!=EOF)
{
int s=0;
scanf("%I64d",&z);
mod=1000000007;
if(z == 1)
{
if(x < 0)
printf("%I64d\n",x+mod);
else
printf("%I64d\n",x);
continue;
}
d.mat[0][1]=-1;d.mat[1][1] = 0;
d.mat[0][0]=d.mat[1][0]=1;
A ret=quickP(z-2);//z-2 乘的次数
__int64 ans=(ret.mat[0][0]*y%mod+ret.mat[0][1]*x%mod)%mod;
if(ans < 0)
ans+=mod;
printf("%I64d\n",ans);
}
return 0;
}
Codeforces Round #257(Div. 2) B. Jzzhu and Sequences(矩阵高速幂)的更多相关文章
- Codeforces Round #257 (Div. 2) B. Jzzhu and Sequences (矩阵快速幂)
题目链接:http://codeforces.com/problemset/problem/450/B 题意很好懂,矩阵快速幂模版题. /* | 1, -1 | | fn | | 1, 0 | | f ...
- 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 ...
- Codeforces Round #257 (Div. 2/B)/Codeforces450B_Jzzhu and Sequences
B解题报告 算是规律题吧,,,x y z -x -y -z 注意的是假设数是小于0,要先对负数求模再加模再求模,不能直接加mod,可能还是负数 给我的戳代码跪了,,. #include <ios ...
- Codeforces Round #257 (Div. 1)449A - Jzzhu and Chocolate(贪婪、数学)
主题链接:http://codeforces.com/problemset/problem/449/A ------------------------------------------------ ...
- Codeforces Round #257 (Div. 2) A. Jzzhu and Children(简单题)
题目链接:http://codeforces.com/problemset/problem/450/A ------------------------------------------------ ...
- Codeforces Round #257 (Div. 1) C. Jzzhu and Apples (素数筛)
题目链接:http://codeforces.com/problemset/problem/449/C 给你n个数,从1到n.然后从这些数中挑选出不互质的数对最多有多少对. 先是素数筛,显然2的倍数的 ...
- Codeforces Round #257 (Div. 1) D - Jzzhu and Numbers 容斥原理 + SOS dp
D - Jzzhu and Numbers 这个容斥没想出来... 我好菜啊.. f[ S ] 表示若干个数 & 的值 & S == S得 方案数, 然后用这个去容斥. 求f[ S ] ...
- Codeforces Round #257 (Div. 2) C. Jzzhu and Chocolate
C. Jzzhu and Chocolate time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Codeforces Round #257 (Div. 2) A. Jzzhu and Children
A. Jzzhu and Children time limit per test 1 second memory limit per test 256 megabytes input standar ...
随机推荐
- 国内物联网平台初探(三) ——QQ物联·智能硬件开放平台
平台定位 将QQ帐号体系.好友关系链.QQ消息通道及音视频服务等核心能力提供给可穿戴设备.智能家居.智能车载.传统硬件等领域的合作伙伴,实现用户与设备.设备与设备.设备与服务之间的联动. 实现用户与设 ...
- Node.js:template
ylbtech-Node.js: 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作者:ylbtech出处:http://ylbtech ...
- [JXOI 2018] 守卫 解题报告 (DP)
interlinkage: https://www.luogu.org/problemnew/show/P4563 description: solution: 注意到对于范围$[l,r]$,$r$这 ...
- python的模块导入
单个文件导入:导入的模块可以是一个py文件(放置在当前文件的同级目录.默认路径等) 导入:import 模块名 使用:模块名.函数名 导入:from 模块名 import * 使用:函数名 ----- ...
- ts中类的继承
定义类 class Person { name: string; //属性 constructor(_name: string) { this.name = _name; } //构造函数 sayHe ...
- JavaScript中的工厂方法、构造函数与class
JavaScript中的工厂方法.构造函数与class 本文转载自:众成翻译 译者:谢于中 链接:http://www.zcfy.cc/article/1129 原文:https://medium.c ...
- 关于H5移动端开发 iPhone X适配
一. 媒体查询. @media screen and (device-width:375px) and (device-height:812px){ #header { height: 88px; p ...
- C# 时间日期(函数,解释)
C#时间/日期格式大全,C#时间/日期函数大全 有时候我们要对时间进行转换,达到不同的显示效果 默认格式为:2005-6-6 14:33:34 如果要换成成200506,06-2005,2005-6- ...
- 互联网的大数据神话——NoSQL
本文摘抄于:<纵横大数据--云计算数据基础设施> 何小朝著 Chapter5. NewSQL--关系数据库联邦/联合 5.4.2 互联网的神话 对强一致性的要求放松,是因为 互联网的分布 ...
- 统计之都 http://cos.name/
http://cos.name/ IMS:一个洲际人际交流网络(为学生免费提供会员资格) 原文链接:http://cos.name/2014/07/ims-a-cross-continent-huma ...