1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| #include <stdio.h>
typedef long long ll; int MOD = 99999989; int inv5 = 50122571;
int qpow(int base, ll p, int mod) { if (p <= 0) return 1; int res = qpow(base, p >> 1, mod); res = res * 1ll * res % mod; if (p & 1) res = res * 1ll * base % mod; return res; }
int inv(int x, int mod) { return qpow(x, mod - 2, mod); }
int abs(int x, int mod) { return (x % mod + mod) % mod; }
int fib(int n, int mod) { int a = abs(1 + inv5, mod) * 1ll * inv(2, mod) % mod; int b = abs(1 - inv5, mod) * 1ll * inv(2, mod) % mod; return inv(inv5, mod) * 1ll * abs(qpow(a, n, mod) - qpow(b, n, mod), mod) % mod; }
int main() { int n, mod = MOD; scanf("%d", &n); printf("%d\n", fib(n, mod)); return 0; }
|