题目链接:http://codeforces.com/problemset/problem/450/B

  1.  
B. Jzzhu and Sequences
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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).

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).

Sample test(s)
input
  1. 2 3
  2. 3
output
  1. 1
input
  1. 0 -1
  2. 2
output
  1. 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).

代码例如以下:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. using namespace std;
  5. struct A
  6. {
  7. int mat[2][2];
  8. };
  9. A d,f;
  10. __int64 n,mod;
  11. A mul(A a,A b)
  12. {
  13. A t;
  14. memset(t.mat,0,sizeof(t.mat));
  15. for(int i = 0; i < n; i++)
  16. {
  17. for(int j = 0; j < n;j++)
  18. {
  19. // if(a.mat[i][k])
  20. for(int k = 0; k < n; k++)
  21. {
  22. t.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
  23. t.mat[i][j]%=mod;
  24. }
  25. }
  26. }
  27. return t;
  28. }
  29. A quickP(int k)
  30. {
  31. A p = d ,m;
  32. memset(m.mat,0,sizeof(m.mat));
  33. for(int i=0;i<n;++i)//单位矩阵
  34. {
  35. m.mat[i][i]=1;
  36. }
  37. while(k)
  38. {
  39. if(k & 1)
  40. m=mul(m,p);
  41. p=mul(p,p);
  42. k >>= 1 ;
  43. }
  44. return m;
  45. }
  46. int main()
  47. {
  48. n=2;
  49. int k,t;__int64 x,y,z;
  50. while(scanf("%I64d%I64d",&x,&y)!=EOF)
  51. {
  52. int s=0;
  53. scanf("%I64d",&z);
  54. mod=1000000007;
  55. if(z == 1)
  56. {
  57. if(x < 0)
  58. printf("%I64d\n",x+mod);
  59. else
  60. printf("%I64d\n",x);
  61. continue;
  62. }
  63. d.mat[0][1]=-1;d.mat[1][1] = 0;
  64. d.mat[0][0]=d.mat[1][0]=1;
  65. A ret=quickP(z-2);//z-2 乘的次数
  66. __int64 ans=(ret.mat[0][0]*y%mod+ret.mat[0][1]*x%mod)%mod;
  67. if(ans < 0)
  68. ans+=mod;
  69. printf("%I64d\n",ans);
  70. }
  71. return 0;
  72. }

Codeforces Round #257(Div. 2) B. Jzzhu and Sequences(矩阵高速幂)的更多相关文章

  1. Codeforces Round #257 (Div. 2) B. Jzzhu and Sequences (矩阵快速幂)

    题目链接:http://codeforces.com/problemset/problem/450/B 题意很好懂,矩阵快速幂模版题. /* | 1, -1 | | fn | | 1, 0 | | f ...

  2. 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 ...

  3. Codeforces Round #257 (Div. 2/B)/Codeforces450B_Jzzhu and Sequences

    B解题报告 算是规律题吧,,,x y z -x -y -z 注意的是假设数是小于0,要先对负数求模再加模再求模,不能直接加mod,可能还是负数 给我的戳代码跪了,,. #include <ios ...

  4. Codeforces Round #257 (Div. 1)449A - Jzzhu and Chocolate(贪婪、数学)

    主题链接:http://codeforces.com/problemset/problem/449/A ------------------------------------------------ ...

  5. Codeforces Round #257 (Div. 2) A. Jzzhu and Children(简单题)

    题目链接:http://codeforces.com/problemset/problem/450/A ------------------------------------------------ ...

  6. Codeforces Round #257 (Div. 1) C. Jzzhu and Apples (素数筛)

    题目链接:http://codeforces.com/problemset/problem/449/C 给你n个数,从1到n.然后从这些数中挑选出不互质的数对最多有多少对. 先是素数筛,显然2的倍数的 ...

  7. Codeforces Round #257 (Div. 1) D - Jzzhu and Numbers 容斥原理 + SOS dp

    D - Jzzhu and Numbers 这个容斥没想出来... 我好菜啊.. f[ S ] 表示若干个数 & 的值 & S == S得 方案数, 然后用这个去容斥. 求f[ S ] ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 基本的Mysql语句

    操作文件夹(库) 增 create database db1 charset utf8; 查 # 查看当前创建的数据库 show create database db1; # 查看所有的数据库 sho ...

  2. LeetCode Weekly Contest 20

    1. 520. Detect Capital 题目描述的很清楚,直接写,注意:字符串长度为1的时候,大写和小写都是满足要求的,剩下的情况单独判断.还有:我感觉自己写的代码很丑,判断条件比较多,需要改进 ...

  3. HBase编程 API入门系列之modify(管理端而言)(10)

    这里,我带领大家,学习更高级的,因为,在开发中,尽量不能去服务器上修改表. 所以,在管理端来修改HBase表.采用线程池的方式(也是生产开发里首推的) package zhouls.bigdata.H ...

  4. lua单链表实现

    List = {} --创建一个节点 function List.new(val) return {pnext = nil, value = val} end --往一个节点后添加一个节点 funct ...

  5. P1982 小朋友的数字

    题目描述 有 n 个小朋友排成一列.每个小朋友手上都有一个数字,这个数字可正可负.规定每个 小朋友的特征值等于排在他前面(包括他本人)的小朋友中连续若干个(最少有一个)小朋 友手上的数字之和的最大值. ...

  6. JS排序之冒泡排序

    冒泡排序的两种策略: <script>// 第一种思路:// 一个数组中的数据,拿第一个和剩下的依次进行对比,数值小的赋值给第一个,一轮比较过后,则数值小的放在最前边.// 第二轮比较,则 ...

  7. 登录linux,输入ls显示anaconda-ks.cfg cobbler.ks ....., 原因在于root@ ~ / 区别

    今天登录linux测试机,想要创建目录,ls的时候,找不到之前的的目录,才发现是目录不对的问题. 首先,先要弄清楚 [root@330c353813ea ~] 和 [root@330c353813ea ...

  8. jQuery操作DOM知识总结

    jquery操作DOM(节点) 1.创建元素 //$(htmlStr) //htmlStr:html格式的字符串 $("<span>这是一个span元素</span> ...

  9. okhttp3 ExceptionInInitializerError 异常处理

    okhttp3 在Android4.4上出现下面异常 java.lang.ExceptionInInitializerError at okhttp3.OkHttpClient.newSslSocke ...

  10. 四.Windows I/O模型之重叠IO(overlapped)模型

    1.适用于除Windows CE之外的各种Windows平台.在使用这个模型之前应该确保该系统安装了Winsock2.重叠模型的基本设计原理是使用一个重叠的数据结构,一次投递一个或多个Winsock ...