Input
1 2 3
1 2 5
Output
4
7
Hint
1 2 3 4
7 12 19 30
49 80 129 208
Solution
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
typedef long long int LLI;
const int MOD = 1000000007;
const int N = 4;
LLI l,r,t,n;
struct Matrix {
LLI mat[N][N];
Matrix operator*(const Matrix& m)const {
Matrix tmp;
for(LLI i = 0 ; i < N ; i++) {
for(LLI j = 0 ; j < N ; j++) {
tmp.mat[i][j] = 0;
for(LLI k = 0 ; k < N ; k++)
tmp.mat[i][j] = (tmp.mat[i][j] + mat[i][k]*m.mat[k][j])%MOD;
tmp.mat[i][j] %= MOD;
}
}
return tmp;
}
};
Matrix m;
void print(Matrix d)
{
LLI i,j;
for (i=0;i<4;++i)
{
for (j=0;j<4;++j)
printf("%4lld",d.mat[i][j]);
printf("\n");
}
printf("\n");
}
LLI Pow() {
Matrix ans,tt=m;
for (LLI i=0; i<4; ++i) ans.mat[i][0]=l;
for (LLI i=0; i<4; ++i) ans.mat[i][1]=r;
for (LLI i=0; i<4; ++i) ans.mat[i][2]=l+r;
for (LLI i=0; i<4; ++i) ans.mat[i][3]=l+r+r-1;
n--;
while(n) {
if (n%2) tt = tt*m;
//print(tt);
n /= 2;
m = m*m;
//print(m);
}
ans=ans*tt;
//print(ans);
return ans.mat[0][t]%MOD;
}
int main() {
while(~scanf("%lld%lld",&l,&r)) {
for (LLI i=0; i<4; ++i) m.mat[0][i]=0;
m.mat[1][0]=0;
m.mat[2][0]=m.mat[3][0]=m.mat[1][1]=m.mat[1][2]=m.mat[1][3]=m.mat[3][1]=1;
m.mat[2][1] = m.mat[3][2]=2;
m.mat[2][2] = 3;
m.mat[2][3]=m.mat[3][3]=4;
scanf("%lld", &n);
t=(n-1)%4;
n=(n-1)/4;
if (!n)
{
if (t==3) printf("%lld\n",r+r+l-1);
else if (t==2) printf("%lld\n",r+l);
else if (t==1) printf("%lld\n",r);
else printf("%lld\n",l);
} else printf("%lld\n", Pow());
}
return 0;
}