This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <stdio.h>
typedef long long lld;
const int MOD = 1e9 + 7;
lld N, K;
struct MAT{
int m[2][2];
MAT operator * (const MAT &ot)const{
MAT ret={0, };
int i, j, k;
for (i=0;i<2;i++) for (j=0;j<2;j++) for (k=0;k<2;k++)
ret.m[i][j] = (ret.m[i][j]+(lld)m[i][k]*ot.m[k][j]%MOD)%MOD;
return ret;
}
} P[40];
struct TERM{
TERM(lld p, lld q){
a = p%MOD, b = q%MOD;
}
int a, b;
TERM operator * (const TERM &ot)const{
return TERM((((lld)a*ot.a%MOD+(lld)a*ot.b%MOD)%MOD+(lld)b*ot.a%MOD)%MOD, ((lld)a*ot.a%MOD+(lld)b*ot.b%MOD)%MOD);
}
};
int fibo(lld n)
{
MAT t ={1, 0, 0, 1};
if (n < 0) return 1;
for (int i=0;i<40;i++,n>>=1) if (n&1) t = t*P[i];
return t.m[0][1];
}
TERM pow(TERM a, lld b)
{
TERM v = a, ret(0, 1);
for (;b;b>>=1, v=v*v) if (b&1) ret = ret*v;
return ret;
}
int pow(int a, lld b)
{
int v = a, ret = 1;
for (;b;b>>=1,v=(lld)v*v%MOD) if (b&1) ret = (lld)ret*v%MOD;
return ret;
}
int main()
{
int i;
MAT t ={1, 1, 1, 0};
P[0] = t;
for (i=1;i<40;i++) P[i] = P[i-1]*P[i-1];
scanf("%lld%lld", &N, &K);
TERM p(fibo(N), fibo(N-1));
TERM q = pow(p, K);
int x = (lld)q.a*pow(fibo(K), MOD-2)%MOD;
int y = (q.b-(lld)x*fibo(K-1)%MOD+MOD)%MOD;
printf("%d %d\n", x, y);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |