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
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// tested by Hightail: https://github.com/dj3500/hightail
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <stack>
#include <cstring>
#include <iomanip>
#include <ctime>
#include <cassert>
using namespace std;
#define pb push_back
#define INF 1001001001
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define FORI(i,n) for(int (i)=1;(i)<=(n);++(i))
#define mp make_pair
#define ll long long
#define SZ(x) ((int)((x).size()))
#define fi first
#define se second
template<typename T,typename TT> ostream& operator<<(ostream &s,pair<T,TT> t) {return s<<"("<<t.first<<","<<t.second<<")";}
template<typename T> ostream& operator<<(ostream &s,vector<T> t){FOR(i,SZ(t))s<<t[i]<<" ";return s; }
#define DBG(vari) cout<<"["<<__LINE__<<"] "<<#vari<<" = "<<(vari)<<endl;
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (__typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define TESTS wez(testow)while(testow--)
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define REMAX(a,b) (a)=max((a),(b));
#define REMIN(a,b) (a)=min((a),(b));
#define IOS ios_base::sync_with_stdio(0);

// funkcja od pana Straszaka
typedef unsigned long long ull;
ull multbig(ull a, ull b, ull m) //mnozy (a*b)%m dla a,b<2^63
{
    if (b==0 || a==0) return 0;
    if (b<(1LL<<31) && a<(1LL<<31))
        return (a*b)%m;
    ull x=0;
    while (b)
    {
        if (b&1)
            x=(x+a)%m;
        a=(a<<1)%m;
        b=b>>1;
    }
    return x;
}

typedef vector<ll> row;
constexpr int R = 2;
ll mod;
struct macierz : vector<row> {
   macierz() : vector<row>(R,row(R,0)) {}
};

macierz id() {
   macierz w;
   FOR(i,R) w[i][i] = 1;
   return w;
}

macierz mult(const macierz &m1, const macierz &m2) {
   macierz w;
   FOR(i,R) FOR(j,R) {
      ll x = 0;
      FOR(k,R) x += multbig(m1[i][k], m2[k][j], mod);
      w[i][j] = x % mod;
   }
   return w;
}

macierz potega(const macierz &m, ll k) {
   if (!k) return id();
   macierz x = potega(m,k/2);
   x = mult(x,x);
   if (k&1) return mult(m,x);
   return x;
}

// przyklad
ll fib (ll k) {
   macierz m;
   m[0][0] = m[0][1] = m[1][0] = 1;
   return potega(m,k)[1][0];
}

ll period[20];

int main () {
   period[0] = 1;
   period[1] = 60;
   period[2] = 300;
   period[3] = 1500;
   REP(k,4,18) period[k] = period[k-1] * 10;

   string digits;
   cin >> digits;
   reverse(ALL(digits));
   int k = SZ(digits);

   vector<ll> cand = {0};
   ll num = 0;
   mod = 1;

   REP(d,1,k) {
      ll newdigit = digits[d-1] - '0';
      FOR(u,d-1) newdigit *= 10;
      num += newdigit;
      mod *= 10;

      vector<ll> newcand;
      for (ll x : cand) {
         for (ll y = x; y < period[d]; y += period[d-1]) {
            if (num == fib(y)) newcand.pb(y);
         }
      }

      swap(cand, newcand);
      // cand = { 0 <= x < period[d] : F_x = n (mod 10^d) }
   }

   if (cand.empty()) {
      printf("NIE");
   } else {
      // wypisz res + 1500...0, zeby sie nie czepiali
      cout << (cand[0] + period[18]);
   }
}