Jzzhu and Sequences (矩阵快速幂 + 取模)

题目链接

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

Examples

Input

2 33

Output

1

Input

0 -12

Output

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

AC代码:

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 using namespace std;
 5 struct matrix{
 6     long long int mat[3][3];
 7 }A,B;
 8 long long int mod(long long int n){
 9     if(n > 0)
10         return n % 1000000007;
11     else if(n < 0)
12         return (n + 1000000007) % 1000000007;
13     else
14         return 0;
15 }
16 matrix mul(matrix a,matrix b){
17     matrix tmp;
18     memset(tmp.mat,0,sizeof(tmp.mat));
19     for(int i = 0;i < 3;i++)
20         for(int j = 0;j < 3;j++)
21             for(int k = 0;k < 3;k++){
22                 tmp.mat[i][j] += a.mat[i][k] * b.mat[k][j];
23                 tmp.mat[i][j] = mod(tmp.mat[i][j]);
24             }
25     return tmp;
26 }
27 matrix pow_mat(matrix a,int n){
28     matrix ans;
29     memset(ans.mat,0,sizeof(ans.mat));
30     for(int i = 0;i < 3;i++)
31         ans.mat[i][i] = 1;
32     while(n){
33         if(n & 1)
34             ans = mul(ans,a);
35         a = mul(a,a);
36         n >>= 1;
37     }
38     return ans;
39 }
40 int main(){
41     long long int x,y,n;
42     scanf("%lld%lld%lld",&x,&y,&n);
43     if(n == 1)
44         printf("%lld\n",mod(x));
45     else if(n == 2)
46         printf("%lld\n",mod(y));
47     else if(n == 3)
48         printf("%lld\n",mod(y - x + 1000000007)); //注意这里的取模
49     else{
50         A.mat[0][0] = x,A.mat[0][1] = y,A.mat[0][2] = y - x;
51         B.mat[0][0] = 0,B.mat[0][1] = 0,B.mat[0][2] = 0;
52         B.mat[1][0] = 1,B.mat[1][1] = 0,B.mat[1][2] = -1;
53         B.mat[2][0] = 0,B.mat[2][1] = 1,B.mat[2][2] = 1;
54         B = pow_mat(B,n - 3);
55         printf("%lld\n",mod(mul(A,B).mat[0][2]));
56     }
57     return 0;
58 }

原文地址:https://www.cnblogs.com/Luckykid/p/9740329.html

时间: 2024-10-15 05:36:12

Jzzhu and Sequences (矩阵快速幂 + 取模)的相关文章

HDU 4965 Fast Matrix Calculation (矩阵快速幂取模----矩阵相乘满足结合律)

http://acm.hdu.edu.cn/showproblem.php?pid=4965 利用相乘的可结合性先算B*A,得到6*6的矩阵,利用矩阵快速幂取模即可水过. 1 #include<iostream> 2 #include<stdio.h> 3 #include<iostream> 4 #include<stdio.h> 5 #define N 1010 6 #define M 1010 7 #define K 6 8 using namespa

POJ 3233-Matrix Power Series( S = A + A^2 + A^3 + … + A^k 矩阵快速幂取模)

Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 20309   Accepted: 8524 Description Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + - + Ak. Input The input contains exactly one test cas

Codeforces 450B div.2 Jzzhu and Sequences 矩阵快速幂or规律

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 i

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

题目链接:http://codeforces.com/problemset/problem/450/B 题意很好懂,矩阵快速幂模版题. 1 /* 2 | 1, -1 | | fn | 3 | 1, 0 | | fn-1 | 4 */ 5 #include <iostream> 6 #include <cstdio> 7 #include <cstring> 8 using namespace std; 9 typedef __int64 LL; 10 LL mod =

Codeforces 450B - Jzzhu and Sequences ( 矩阵快速幂 )

题意: 给定f1和f2,求fn 分析: 特判f1,f2 当n>=3时使用矩阵快速幂即可( 简单题 ) 将公式转化一下 , 可以得到一个变换矩阵 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define CLR( a, b ) memset( a, b, sizeof(a) ) #define MAT_SIZE 2 #define MOD 10

hdu 3221 Brute-force Algorithm(快速幂取模,矩阵快速幂求fib)

http://acm.hdu.edu.cn/showproblem.php?pid=3221 一晚上搞出来这么一道题..Mark. 给出这么一个程序,问funny函数调用了多少次. 我们定义数组为所求:f[1] = a,f[2] = b, f[3] = f[2]*f[3]......f[n] = f[n-1]*f[n-2].对应的值表示也可为a^1*b^0%p,a^0*b^1%p,a^1*b^1%p,.....a^fib[n-3]*b^fib[n-2]%p.即a,b的指数从n=3以后与fib数列

HDU 4365 正方形格子涂色中心对称轴对称的涂法有多少种-思维-(矩阵坐标关系&amp;快速幂取模)

题意:n*n的格子,涂色,有k种颜料,必须满足旋转任意个90度和翻转之后图片的样子不变,现在已经有m个格子涂过色了,问还有多少种涂法满足上述条件. 分析: 满足上述对称条件,那么涂色的种类问题我们可以放在正方形的一个角来做,因为一个角确定了其他角的颜色也就确定了. 以左上角的下半角为例.共有1+2+....+(n+1)/2个格子,然后记录涂过色的格子对应到这个三角形里的格子数目,用tot来记录,即每输入一个涂过色的格子的坐标我们就在这个三角形里找与之对应的坐标,用vis[][]数组标记是否已经计

HDU - 2817 - A sequence of numbers (快速幂取模!)

A sequence of numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3494    Accepted Submission(s): 1073 Problem Description Xinlv wrote some sequences on the paper a long time ago, they migh

杭电 2817 A sequence of numbers【快速幂取模】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2817 解题思路:arithmetic or geometric sequences 是等差数列和等比数列的意思, 即令输入的第一个数为a(1),那么对于等差数列 a(k)=a(1)+(k-1)*d,即只需要求出 a(k)%mod   又因为考虑到k和a的范围, 所以对上式通过同余作一个变形:即求出 (a(1)%mod+(k-1)%mod*(d%mod))%mod 对于等比数列 a(k)=a(1)*q