#pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("inline") #include using namespace std; #define MD (10U) template struct cLtraits_identity{ using type = T; } ; template using cLtraits_try_make_signed = typename conditional< is_integral::value, make_signed, cLtraits_identity >::type; template struct cLtraits_common_type{ using tS = typename cLtraits_try_make_signed::type; using tT = typename cLtraits_try_make_signed::type; using type = typename common_type::type; } ; void*wmem; char memarr[96000000]; template inline auto max_L(S a, T b) -> typename cLtraits_common_type::type{ return (typename cLtraits_common_type::type) a >= (typename cLtraits_common_type::type) b ? a : b; } template inline void walloc1d(T **arr, int x, void **mem = &wmem){ static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] ); (*arr)=(T*)(*mem); (*mem)=((*arr)+x); } template inline void walloc1d(T **arr, int x1, int x2, void **mem = &wmem){ walloc1d(arr, x2-x1, mem); (*arr) -= x1; } struct Modint{ unsigned val; Modint(){ val=0; } Modint(int a){ val = ord(a); } Modint(unsigned a){ val = ord(a); } Modint(long long a){ val = ord(a); } Modint(unsigned long long a){ val = ord(a); } inline unsigned ord(unsigned a){ return a%MD; } inline unsigned ord(int a){ a %= (int)MD; if(a < 0){ a += MD; } return a; } inline unsigned ord(unsigned long long a){ return a%MD; } inline unsigned ord(long long a){ a %= (int)MD; if(a < 0){ a += MD; } return a; } inline unsigned get(){ return val; } inline Modint &operator++(){ val++; if(val >= MD){ val -= MD; } return *this; } inline Modint &operator--(){ if(val == 0){ val = MD - 1; } else{ --val; } return *this; } inline Modint operator++(int a){ Modint res(*this); val++; if(val >= MD){ val -= MD; } return res; } inline Modint operator--(int a){ Modint res(*this); if(val == 0){ val = MD - 1; } else{ --val; } return res; } inline Modint &operator+=(Modint a){ val += a.val; if(val >= MD){ val -= MD; } return *this; } inline Modint &operator-=(Modint a){ if(val < a.val){ val = val + MD - a.val; } else{ val -= a.val; } return *this; } inline Modint &operator*=(Modint a){ val = ((unsigned long long)val*a.val)%MD; return *this; } inline Modint &operator/=(Modint a){ return *this *= a.inverse(); } inline Modint operator+(Modint a){ return Modint(*this)+=a; } inline Modint operator-(Modint a){ return Modint(*this)-=a; } inline Modint operator*(Modint a){ return Modint(*this)*=a; } inline Modint operator/(Modint a){ return Modint(*this)/=a; } inline Modint operator+(int a){ return Modint(*this)+=Modint(a); } inline Modint operator-(int a){ return Modint(*this)-=Modint(a); } inline Modint operator*(int a){ return Modint(*this)*=Modint(a); } inline Modint operator/(int a){ return Modint(*this)/=Modint(a); } inline Modint operator+(long long a){ return Modint(*this)+=Modint(a); } inline Modint operator-(long long a){ return Modint(*this)-=Modint(a); } inline Modint operator*(long long a){ return Modint(*this)*=Modint(a); } inline Modint operator/(long long a){ return Modint(*this)/=Modint(a); } inline Modint operator-(void){ Modint res; if(val){ res.val=MD-val; } else{ res.val=0; } return res; } inline operator bool(void){ return val!=0; } inline operator int(void){ return get(); } inline operator long long(void){ return get(); } inline Modint inverse(){ int a = val; int b = MD; int u = 1; int v = 0; int t; Modint res; while(b){ t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } if(u < 0){ u += MD; } res.val = u; return res; } inline Modint pw(unsigned long long b){ Modint a(*this); Modint res; res.val = 1; while(b){ if(b&1){ res *= a; } b >>= 1; a *= a; } return res; } inline bool operator==(int a){ return ord(a)==val; } inline bool operator!=(int a){ return ord(a)!=val; } } ; inline Modint operator+(int a, Modint b){ return Modint(a)+=b; } inline Modint operator-(int a, Modint b){ return Modint(a)-=b; } inline Modint operator*(int a, Modint b){ return Modint(a)*=b; } inline Modint operator/(int a, Modint b){ return Modint(a)/=b; } inline Modint operator+(long long a, Modint b){ return Modint(a)+=b; } inline Modint operator-(long long a, Modint b){ return Modint(a)-=b; } inline Modint operator*(long long a, Modint b){ return Modint(a)*=b; } inline Modint operator/(long long a, Modint b){ return Modint(a)/=b; } inline int my_getchar_unlocked(){ static char buf[1048576]; static int s = 1048576; static int e = 1048576; if(s == e && e == 1048576){ e = fread_unlocked(buf, 1, 1048576, stdin); s = 0; } if(s == e){ return EOF; } return buf[s++]; } inline void rd(long long &x){ int k; int m=0; x=0; for(;;){ k = my_getchar_unlocked(); if(k=='-'){ m=1; break; } if('0'<=k&&k<='9'){ x=k-'0'; break; } } for(;;){ k = my_getchar_unlocked(); if(k<'0'||k>'9'){ break; } x=x*10+k-'0'; } if(m){ x=-x; } } struct MY_WRITER{ char buf[1048576]; int s; int e; MY_WRITER(){ s = 0; e = 1048576; } ~MY_WRITER(){ if(s){ fwrite_unlocked(buf, 1, s, stdout); } } } ; MY_WRITER MY_WRITER_VAR; void my_putchar_unlocked(int a){ if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){ fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout); MY_WRITER_VAR.s = 0; } MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a; } inline void wt_L(char a){ my_putchar_unlocked(a); } inline void wt_L(int x){ int s=0; int m=0; char f[10]; if(x<0){ m=1; x=-x; } while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } if(m){ my_putchar_unlocked('-'); } while(s--){ my_putchar_unlocked(f[s]+'0'); } } inline void wt_L(unsigned x){ int s=0; char f[10]; while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } while(s--){ my_putchar_unlocked(f[s]+'0'); } } inline void wt_L(long long x){ int s=0; int m=0; char f[20]; if(x<0){ m=1; x=-x; } while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } if(m){ my_putchar_unlocked('-'); } while(s--){ my_putchar_unlocked(f[s]+'0'); } } inline void wt_L(unsigned long long x){ int s=0; char f[21]; while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } while(s--){ my_putchar_unlocked(f[s]+'0'); } } inline void wt_L(Modint x){ int i; i = (int)x; wt_L(i); } int WRITER_DOUBLE_DIGIT = 15; inline int writerDigit_double(){ return WRITER_DOUBLE_DIGIT; } inline void writerDigit_double(int d){ WRITER_DOUBLE_DIGIT = d; } inline void wt_L(double x){ const int d = WRITER_DOUBLE_DIGIT; int k; int r; double v; if(x!=x || (x==x+1 && x==2*x)){ my_putchar_unlocked('E'); my_putchar_unlocked('r'); my_putchar_unlocked('r'); return; } if(x < 0){ my_putchar_unlocked('-'); x = -x; } x += 0.5 * pow(0.1, d); r = 0; v = 1; while(x >= 10*v){ v *= 10; r++; } while(r >= 0){ r--; k = floor(x / v); if(k >= 10){ k = 9; } if(k <= -1){ k = 0; } x -= k * v; v *= 0.1; my_putchar_unlocked(k + '0'); } if(d > 0){ my_putchar_unlocked('.'); v = 1; for(r=(0);r<(d);r++){ v *= 0.1; k = floor(x / v); if(k >= 10){ k = 9; } if(k <= -1){ k = 0; } x -= k * v; my_putchar_unlocked(k + '0'); } } } inline void wt_L(const char c[]){ int i=0; for(i=0;c[i]!='\0';i++){ my_putchar_unlocked(c[i]); } } inline void wt_L(string &x){ int i=0; for(i=0;x[i]!='\0';i++){ my_putchar_unlocked(x[i]); } } template T PowMod(T a, P b, M m){ T r; r = 1; while(b > 0){ if(b % 2){ r = r * a % m; } b /= 2; if(b > 0){ a = a * a % m; } } return r; } template inline S chmax(S &a, T b){ if(a struct Polynomial{ int d; int mem; T*c; Polynomial(){ mem = 1; c = new T[mem]; d = 0; c[0] = 0; } Polynomial(T a){ mem = 1; c = new T[mem]; d = 0; c[0] = a; } Polynomial(const Polynomial &a){ int i; d = a.d; mem = d + 1; c = new T[mem]; for(i=(0);i<(d+1);i++){ c[i] = a.c[i]; } } ~Polynomial(){ delete [] c; } void expand(int z){ int i; T*cc; if(z <= mem){ return; } mem =max_L(z, 2 * mem); cc = new T[mem]; for(i=(0);i<(d+1);i++){ cc[i] = c[i]; } delete [] c; c = cc; } inline void change(const int dg, const T cf){ expand(dg+1); while(d < dg){ c[++d] = 0; } c[dg] = cf; while(d && c[d]==0){ d--; } } inline int deg(void){ return d; } inline T coef(const int k){ if(k > d){ return 0; } return c[k]; } Polynomial& operator=(const T a){ d = 0; expand(d + 1); c[0] = a; return *this; } Polynomial& operator=(const Polynomial &a){ int i; d = a.d; expand(d + 1); for(i=(0);i<(d+1);i++){ c[i] = a.c[i]; } return *this; } Polynomial& operator+=(const Polynomial &a){ int i; int k; k =max_L(d, a.d); expand(k+1); while(d < k){ c[++d] = 0; } for(i=(0);i<(a.d+1);i++){ c[i] += a.c[i]; } while(d && c[d]==0){ d--; } return *this; } Polynomial operator+(const Polynomial &a){ return Polynomial(*this) += a; } Polynomial& operator-=(const Polynomial &a){ int i; int k; k =max_L(d, a.d); expand(k+1); while(d < k){ c[++d] = 0; } for(i=(0);i<(a.d+1);i++){ c[i] -= a.c[i]; } while(d && c[d]==0){ d--; } return *this; } Polynomial operator-(const Polynomial &a){ return Polynomial(*this) -= a; } Polynomial& operator*=(const Polynomial &a){ int i; int j; int k; T*cc; void*mem = wmem; k = d + a.d; expand(k+1); walloc1d(&cc, k+1, &mem); for(i=(0);i<(k+1);i++){ cc[i] = 0; } for(i=(0);i<(d+1);i++){ for(j=(0);j<(a.d+1);j++){ cc[i+j] += c[i] * a.c[j]; } } for(i=(0);i<(k+1);i++){ c[i] = cc[i]; } d = k; while(d && c[d]==0){ d--; } return *this; } Polynomial operator*(const Polynomial &a){ return Polynomial(*this) *= a; } Polynomial& operator/=(const Polynomial &a){ int i; int j; int k; T*cc; T e; void*mem = wmem; walloc1d(&cc, d-a.d, &mem); for(i=d; i>=a.d; i--){ cc[i-a.d] = e = c[i] / a.c[a.d]; for(j=(0);j<(a.d+1);j++){ c[i-j] -= e * a.c[a.d-j]; } } d -= a.d; for(i=(0);i<(d+1);i++){ c[i] = cc[i]; } return *this; } Polynomial operator/(const Polynomial &a){ return Polynomial(*this) /= a; } Polynomial& operator%=(const Polynomial &a){ int i; int j; int k; T*cc; T e; void*mem = wmem; walloc1d(&cc, d-a.d, &mem); for(i=d; i>=a.d; i--){ cc[i-a.d] = e = c[i] / a.c[a.d]; for(j=(0);j<(a.d+1);j++){ c[i-j] -= e * a.c[a.d-j]; } } while(d && c[d]==0){ d--; } return *this; } Polynomial operator%(const Polynomial &a){ return Polynomial(*this) %= a; } Polynomial& operator*=(const T &a){ int i; for(i=(0);i<(d+1);i++){ c[i] *= a; } while(d && c[d]==0){ d--; } return *this; } Polynomial operator*(const T &a){ return Polynomial(*this) *= a; } Polynomial& operator/=(const T &a){ int i; for(i=(0);i<(d+1);i++){ c[i] /= a; } while(d && c[d]==0){ d--; } return *this; } Polynomial operator/(const T &a){ return Polynomial(*this) /= a; } inline T operator()(const T x){ int i; T res; res = 0; for(i=d;i>=0;i--){ res = res * x + c[i]; } return res; } } ; template Polynomial operator*(const T a, const Polynomial &b){ return Polynomial(b)*=a; } int main(){ wmem = memarr; long long p; rd(p); long long q; rd(q); long long r; rd(r); long long k; rd(k); Polynomial a; Polynomial m; Polynomial z; a.change(1,1); m.change(3,1); m.change(2,-1); m.change(1,-1); m.change(0,-1); z=PowMod(a,k-1,m); wt_L(z.coef(0)*p+z.coef(1)*q+z.coef(2)*r); wt_L('\n'); return 0; } // cLay version 20210708-1 // --- original code --- // #define MD 10 // { // ll@p,@q,@r,@k; // Polynomiala,m,z; // a.change(1,1); // m.change(3,1); // m.change(2,-1); // m.change(1,-1); // m.change(0,-1); // z=PowMod(a,k-1,m); // wt(z.coef(0)*p+z.coef(1)*q+z.coef(2)*r); // }