H.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long LL;
  4. #define FOR(i, x, y) for (LL i = (x), _##i = (y); i < _##i; ++i)
  5. #define FORD(i, x, y) for (LL i = (x), _##i = (y); i > _##i; --i)
  6. struct Mat {
  7. static const int MOD = 10000;
  8. static const LL M = 2;
  9. LL v[M][M];
  10. Mat() { memset(v, 0, sizeof(v)); }
  11. void eye() { FOR(i, 0, M)v[i][i] = 1; }
  12. LL *operator[](LL x) { return v[x]; }
  13. const LL *operator[](LL x) const { return v[x]; }
  14. Mat operator*(const Mat &B) {
  15. const Mat &A = *this;
  16. Mat ret;
  17. FOR (k, 0, M)
  18. FOR (i, 0, M) if (A[i][k])
  19. FOR (j, 0, M)ret[i][j] = (ret[i][j] + A[i][k] * B[k][j]) % MOD;
  20. return ret;
  21. }
  22. Mat pow(LL n) const {
  23. Mat A = *this, ret;
  24. ret.eye();
  25. for (; n; n >>= 1, A = A * A)
  26. if (n & 1) ret = ret * A;
  27. return ret;
  28. }
  29. Mat operator+(const Mat &B) {
  30. const Mat &A = *this;
  31. Mat ret;
  32. FOR (i, 0, M)FOR (j, 0, M)ret[i][j] = (A[i][j] + B[i][j]) % MOD;
  33. return ret;
  34. }
  35. void prt() const {
  36. FOR (i, 0, M)FOR (j, 0, M)printf("%lld%c", (*this)[i][j], j == M - 1 ? '\n' : ' ');
  37. }
  38. };
  39. int main() {
  40. Mat m;
  41. m[0][0] = m[0][1] = m[1][0] = 1;
  42. m[1][1] = 0;
  43. for (int n; cin >> n and n != -1;) {
  44. if (n == 0) {
  45. cout << 0 << endl;
  46. continue;
  47. }
  48. cout << m.pow(n - 1)[0][0] << endl;
  49. }
  50. return 0;
  51. }