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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//Piotr Golda
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <string>
using namespace std;

#define ULLD unsigned long long int
#define UD unsigned int
#define FIRSTAMOUNT 84 //count of first fibonacci numbers
#define FLAG 18446744073709551615

ULLD fibonacci(ULLD n, UD k);

vector<char> seq; // searched sequence
ULLD FirstNums[FIRSTAMOUNT]; // first fibonacci numbers
ULLD Bounds[18];

void scanAndInit()
{
	string s;
	cin >> s;
	for (UD i = 0; i < s.length(); ++i)
		seq.push_back(s[i] - '0');
	FirstNums[0] = 0;
	FirstNums[1] = 1;
	for (UD i = 2; i < FIRSTAMOUNT; ++i)
	{
		FirstNums[i] = FirstNums[i - 1] + FirstNums[i - 2];
	}
	Bounds[0] = 60;
	Bounds[1] = 300;
	Bounds[2] = 1500;
	for (UD i = 3; i < 18; ++i)
		Bounds[i] = Bounds[i - 1] * 10L;
}


static inline UD fibDigits(ULLD p_num)
{
	if (p_num == 0)
		return 1;
	if (p_num > 83)
		return 18;
	UD c = 0;
	ULLD num = FirstNums[p_num];
	while (num>0)
	{
		c++;
		num /= 10L;
	}
	return c;
}

//searches for a kth from the end digit
ULLD searchKth(UD p_k, ULLD p_step, ULLD p_fibNum, ULLD p_tenPow)
{
	ULLD x;
	while (p_fibNum < Bounds[p_k])
	{
		x = fibonacci(p_fibNum, p_k+1);
		x /= p_tenPow; // p_fibNum-th p_k-th digit from the end
		if ((char)x == seq[seq.size() - p_k - 1])
		{
			if (p_k == seq.size() - 1)
				return p_fibNum;
			x = searchKth(p_k + 1, Bounds[p_k], p_fibNum, p_tenPow * 10L);
			if (x != FLAG)
				return x;
		}
		p_fibNum += p_step;
	}
	return FLAG;
}

void findNumber() 
{
	if (seq.size() == 1 && seq[0] == 0)
	{
		cout << 0 << endl;
		return;
	}
	ULLD x;
	x = searchKth(0, 1, 0, 1);
	if (x == FLAG)
		cout << "NIE" << endl;
	else
	{
		if (fibDigits(x) < seq.size())
		{
			x += Bounds[seq.size() - 1];
		}
		cout << x << endl;
	}
}




int main()
{
	ios_base::sync_with_stdio(0);
	scanAndInit();
	findNumber();

	return 0;
}

/*
*****************************************************************************
**********************k digits of n-th Fibonacci number**********************
*****************************************************************************
*/


static inline ULLD mulmod(ULLD a, ULLD b, ULLD m)
{
	ULLD rv = 0;
	while (b)
	{
		if (b & 1) { rv += a; if (rv >= m) rv -= m; }
		a += a; if (a >= m) a -= m;
		b >>= 1;
	}
	return rv;
}

inline void fibmul(ULLD* f, ULLD* g, ULLD& modk)
{
	ULLD tmp = (mulmod(f[0], g[0], modk) + mulmod(f[1], g[1], modk)) % modk;
	f[1] = (mulmod(f[0], g[1], modk) + mulmod(f[1], (g[0] + g[1])%modk, modk)) % modk;
	f[0] = tmp;
}

ULLD fibonacci(ULLD n, UD k)
{
	ULLD modk = 1;
	while (k--)
		modk *= 10L;
	ULLD f[] = { 1, 0 };
	ULLD g[] = { 0, 1 };
	while (n > 0)
	{
		if (n & 1) // n odd
		{
			fibmul(f, g, modk);
			--n;
		}
		else
		{
			fibmul(g, g, modk);
			n >>= 1;
		}
	}
	return f[1];
}