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 ...
随机推荐
- 基本的Mysql语句
操作文件夹(库) 增 create database db1 charset utf8; 查 # 查看当前创建的数据库 show create database db1; # 查看所有的数据库 sho ...
- LeetCode Weekly Contest 20
1. 520. Detect Capital 题目描述的很清楚,直接写,注意:字符串长度为1的时候,大写和小写都是满足要求的,剩下的情况单独判断.还有:我感觉自己写的代码很丑,判断条件比较多,需要改进 ...
- HBase编程 API入门系列之modify(管理端而言)(10)
这里,我带领大家,学习更高级的,因为,在开发中,尽量不能去服务器上修改表. 所以,在管理端来修改HBase表.采用线程池的方式(也是生产开发里首推的) package zhouls.bigdata.H ...
- lua单链表实现
List = {} --创建一个节点 function List.new(val) return {pnext = nil, value = val} end --往一个节点后添加一个节点 funct ...
- P1982 小朋友的数字
题目描述 有 n 个小朋友排成一列.每个小朋友手上都有一个数字,这个数字可正可负.规定每个 小朋友的特征值等于排在他前面(包括他本人)的小朋友中连续若干个(最少有一个)小朋 友手上的数字之和的最大值. ...
- JS排序之冒泡排序
冒泡排序的两种策略: <script>// 第一种思路:// 一个数组中的数据,拿第一个和剩下的依次进行对比,数值小的赋值给第一个,一轮比较过后,则数值小的放在最前边.// 第二轮比较,则 ...
- 登录linux,输入ls显示anaconda-ks.cfg cobbler.ks ....., 原因在于root@ ~ / 区别
今天登录linux测试机,想要创建目录,ls的时候,找不到之前的的目录,才发现是目录不对的问题. 首先,先要弄清楚 [root@330c353813ea ~] 和 [root@330c353813ea ...
- jQuery操作DOM知识总结
jquery操作DOM(节点) 1.创建元素 //$(htmlStr) //htmlStr:html格式的字符串 $("<span>这是一个span元素</span> ...
- okhttp3 ExceptionInInitializerError 异常处理
okhttp3 在Android4.4上出现下面异常 java.lang.ExceptionInInitializerError at okhttp3.OkHttpClient.newSslSocke ...
- 四.Windows I/O模型之重叠IO(overlapped)模型
1.适用于除Windows CE之外的各种Windows平台.在使用这个模型之前应该确保该系统安装了Winsock2.重叠模型的基本设计原理是使用一个重叠的数据结构,一次投递一个或多个Winsock ...