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 ...
随机推荐
- Android平台下的TCP/IP传输(客户端)
在工科类项目中,嵌入式系统与软件系统或后台数据库之间的信息传输是实现“物联网”的一种必要的途径,对已简单概念的物联网,通常形式都是一个单片机/嵌入式系统实现数据的采集及其处理,通过蓝牙,wifi或者是 ...
- 为什么用Mysql?
阅读目录 楔子 初识数据库 为什么要用数据库 认识数据库 初识mysql mysql概念 下载和安装 初识sql语句 楔子 假设现在你已经是某大型互联网公司的高级程序员,让你写一个火车票购票系统,来h ...
- bind()函数的作用
bind()函数是Function原型上的一个属性,当某个函数调用此方法时,可以通过向bind()函数传入执行对象和调用bind的函数的参数来改变函数的执行对象 /*问题:改变func执行环境,使之输 ...
- 「Redis 笔记」常用命令
编号 命令 描述 1 DEL key 此命令删除一个指定键(如果存在). 2 DUMP key 此命令返回存储在指定键的值的序列化版本. 3 EXISTS key 此命令检查键是否存在. 4 EXPI ...
- (转) RabbitMQ学习之helloword(java)
http://blog.csdn.net/zhu_tianwei/article/details/40835555 amqp-client:http://www.rabbitmq.com/java-c ...
- 微信二次认证 C#
using Senparc.Weixin.Entities; using Senparc.Weixin.HttpUtility; using Senparc.Weixin.QY.AdvancedAPI ...
- MySQL主主高可用(keepalive)
2台新的虚拟机172.16.1.1.172.16.1.2 (配置yum源 ) 安装数据库服务 其中 172.16.1.1.172.16.1.2运行数据库服务并设置数据库管理员从本机登录的密码是xzw ...
- 在Linux Centos 7.2 上安装指定版本Docker 17.03
相关资料链接: https://docs.docker.com/install/linux/docker-ce/centos/#install-docker-ce 先清空下“历史” yum insta ...
- MyBatis源码分析(各组件关系+底层原理
MyBatis源码分析MyBatis流程图 下面将结合代码具体分析. MyBatis具体代码分析 SqlSessionFactoryBuilder根据XML文件流,或者Configuration类实例 ...
- SQL2012安装
SQL2012安装 1.打开SQL安装包,点击setup安装 2.选择安装界面,点击全新安装 3.验证通过后,点击确定 4.选择我接受,点击下一步 5.在外网环境,可进行在线更新,内网环境取消勾选产品 ...