Pagini recente » Cod sursa (job #1347041) | Cod sursa (job #2814541) | Cod sursa (job #2275209) | Cod sursa (job #545942) | Cod sursa (job #1842950)
#include <cstdio>
#define MOD 666013
struct mat2
{
mat2() {}
mat2(int diag) { m11 = diag; m12 = 0; m21 = 0; m22 = diag; }
int m11, m12;
int m21, m22;
mat2 operator*(const mat2& a) const
{
mat2 res;
res.m11 = (1LL * m11 * a.m11 + 1LL * m12 * a.m21) % MOD;
res.m12 = (1LL * m11 * a.m12 + 1LL * m12 * a.m22) % MOD;
res.m21 = (1LL * m21 * a.m11 + 1LL * m22 * a.m21) % MOD;
res.m22 = (1LL * m21 * a.m12 + 1LL * m22 * a.m22) % MOD;
return res;
}
mat2& operator*=(const mat2& a)
{
mat2 res = *this * a;
m11 = res.m11;
m12 = res.m12;
m21 = res.m21;
m22 = res.m22;
return *this;
}
};
mat2 pow(mat2 x, int b)
{
mat2 y(1);
while(b != 1)
{
if(b & 1)
{
y *= x;
b ^= 1;
}
else
{
x *= x;
b >>= 1;
}
}
return x * y;
}
int main()
{
freopen("kfib.in", "r", stdin);
freopen("kfib.out", "w", stdout);
mat2 z;
z.m11 = 0;
z.m12 = 1;
z.m21 = 1;
z.m22 = 1;
int n;
scanf("%d", &n);
mat2 znm1 = pow(z, n - 1);
printf("%d\n", znm1.m22);
return 0;
}