Jzzhu and sequences (matrix fast power + modulo)

Source: Internet
Author: User
Tags mul

Topic links

Jzzhu have invented a kind of sequences, they meet the following property:

            

You is given x and y, please calculate fn modulo 1000000007 (Ten9?+?7).

Input

The first line contains 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 (Ten9?+?7).

ExamplesInput
2 3
3
Output
1
Input
0-1
2
Output
1000000006
Note

In the first sample, f2?=? F1?+? F3, 3?=?2?+? F3, f3?=?1.

In the second sample, f2?=?? -?1; ?-? 1 modulo (ten9?+?7) equals (9?+?6).

AC Code:

1#include <iostream>2#include <cstring>3#include <cstdio>4 using namespacestd;5 structmatrix{6     Long Long intmat[3][3];7 }a,b;8 Long Long intMoDLong Long intN) {9     if(N >0)Ten         returnN%1000000007; One     Else if(N <0) A         return(n +1000000007) %1000000007; -     Else -         return 0; the } - Matrix Mul (Matrix A,matrix b) { - matrix tmp; -memset (Tmp.mat,0,sizeof(Tmp.mat)); +      for(inti =0; I <3; i++) -          for(intj =0; J <3; j + +) +              for(intK =0; k <3; k++){ ATMP.MAT[I][J] + = a.mat[i][k] *B.mat[k][j]; atTMP.MAT[I][J] =mod (tmp.mat[i][j]); -             } -     returntmp; - } -Matrix Pow_mat (Matrix A,intN) { - matrix ans; inmemset (Ans.mat,0,sizeof(Ans.mat)); -      for(inti =0; I <3; i++) toAns.mat[i][i] =1; +      while(n) { -         if(N &1) theAns =Mul (ans,a); *A =Mul (a,a); $N >>=1;Panax Notoginseng     } -     returnans; the } + intMain () { A     Long Long intX,y,n; thescanf"%lld%lld%lld",&x,&y,&n); +     if(n = =1) -printf"%lld\n", mod (x)); $     Else if(n = =2) $printf"%lld\n", mod (y)); -     Else if(n = =3) -printf"%lld\n", MoD (Y-x +1000000007));//Notice the modulus here the     Else{ -a.mat[0][0] = x,a.mat[0][1] = y,a.mat[0][2] = y-x;Wuyib.mat[0][0] =0, b.mat[0][1] =0, b.mat[0][2] =0; theb.mat[1][0] =1, b.mat[1][1] =0, b.mat[1][2] = -1; -b.mat[2][0] =0, b.mat[2][1] =1, b.mat[2][2] =1; WuB = Pow_mat (B,n-3); -printf"%lld\n", MoD (Mul (A, b). mat[0][2])); About     } $     return 0; -}
View Code

Jzzhu and sequences (matrix fast power + modulo)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.