#pragma GCC optimize("Ofast") #include //#include //#include //namespace mp=boost::multiprecision; //#define mulint mp::cpp_int //#define mulfloat mp::cpp_dec_float_100 using namespace std; struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<=0;(i)--) #define Flag(x) (1<<(x)) #define Flagcount(x) __builtin_popcountll(x) #define pint pair #define pdouble pair #define plint pair #define fi first #define se second typedef long long lint; int dx[8]={1,1,0,-1,-1,-1,0,1}; int dy[8]={0,1,1,1,0,-1,-1,-1}; const int MAX_N=2e5+5; //struct edge{lint to,num;}; //vector bucket[MAX_N/1000]; typedef vector> mat; mat mtx{ {1,1}, {1,0}, }; vector F{0,1}; //F(1)~F(K)の値 mat mul(mat &A,mat &B){ mat C(A.size(),vector(B[0].size())); rep(i,A.size()) rep(j,B.size()) rep(k,B[0].size()){ C[i][k]=(C[i][k]+A[i][j]*B[j][k])%MOD; //演算 適宜演算子を変える } return C; } mat powmat(mat A,lint n){ mat B(A.size(),vector(A.size())); rep(i,A.size()) B[i][i]=1; //単位元 零元が0じゃない時はそこも自分で埋める while(n){ if(n&1) B=mul(B,A); A=mul(A,A); n>>=1; } return B; } mat powmatsum(mat A,lint n){ //A+A^2+...+A^Nを返す mat B(A.size()*2,vector(A.size()*2)); rep(i,A.size()) rep(j,A.size()) B[i][j]=A[i][j]; rep(i,A.size()) B[A.size()+i][i]=B[A.size()+i][A.size()+i]=1; B=powmat(B,n+1); rep(i,A.size()) B[A.size()+i][i]=(B[A.size()+i][i]-1+MOD)%MOD; mat res(A.size(),vector(A.size())); rep(i,A.size()) rep(j,A.size()) res[i][j]=B[A.size()+i][j]; return res; } lint calc(lint n,mat A=mtx,vector f=F){ //F(N)の計算 if(n==0) return 0; A=powmat(A,n-1); lint res=0; //零元 rep(i,A.size()) res=(res+A[A.size()-1][i]*f[A.size()-1-i])%MOD;//演算 適宜演算子を変える return res; } lint calcsum(lint n,mat A=mtx,vector f=F){ //Σ[i=1..N]F(i)の計算 if(n==0) return 0; mat B(A.size()*2,vector(A.size()*2)); rep(i,A.size()) rep(j,A.size()) B[i][j]=A[i][j]; rep(i,A.size()) B[A.size()+i][i]=B[A.size()+i][A.size()+i]=1; B=powmat(B,n); rep(i,A.size()) B[A.size()+i][i]=(B[A.size()+i][i]-1+MOD)%MOD; lint res=0; //零元 rep(i,A.size()) res=(res+B[A.size()*2-1][i]*f[A.size()-1-i])%MOD;//演算 適宜演算子を変える return res; } /* 定数項がある場合、定数をXとして {1,0,1,X},//F(N-1),F(N-2),...,F(N-K)の係数,X {1,0,0,0}, {0,1,0,0}, {0,0,0,1}. F{1,f1,f2,f3}; 答えの計算パートが rep(i,A.size()) res=(res+A[A.size()-2][i]*F[A.size()-1-i])%MOD; となる 累乗和のときは rep(i,A.size()) res=(res+B[A.size()*2-2][i]*f[A.size()-1-i])%MOD; */ int main(void){ lint N; cin >> mtx[0][0] >> mtx[0][1] >> N; cout << calc(N+1) << endl; }