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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include <cstdio>
#include <cstring>
#include <utility>

//#define YESNO_ONLY

using namespace std;

typedef unsigned long long ULL;

const ULL MOD = 1000LL*1000*1000*1000*1000*1000;

const ULL pow10[] = {\
	1LL,\
	10LL,\
	100LL,\
	1000LL,\
	10000LL,\
	100000LL,\
	1000000LL,\
	10000000LL,\
	100000000LL,\
	1000000000LL,\
	10000000000LL,\
	100000000000LL,\
	1000000000000LL,\
	10000000000000LL,\
	100000000000000LL,\
	1000000000000000LL,\
	10000000000000000LL,\
	100000000000000000LL,\
	1000000000000000000LL,\
};

const ULL cycLen[] = {\
	60LL,\
	300LL,\
	1500LL,\
	15000LL,\
	150000LL,\
	1500000LL,\
	15000000LL,\
	150000000LL,\
	1500000000LL,\
	15000000000LL,\
	150000000000LL,\
	1500000000000LL,\
	15000000000000LL,\
	150000000000000LL,\
	1500000000000000LL,\
	15000000000000000LL,\
	150000000000000000LL,\
	1500000000000000000LL,\
};

const ULL leadingZeroesFix = 1500000000000000000LL;

int sufixSize;
ULL sufix[30]={};

//################################################################
//Safe addition/multiplication modulo MOD:
//################################################################

ULL add(ULL a, ULL b) {
	return (a+b)%MOD;
}

ULL mul(ULL a, ULL b) {
    ULL res = 0;
    while (a != 0) {
        if (a & 1) res = (res + b) % MOD;
        a >>= 1;
        b = (b << 1) % MOD;
    }
    return res;
}

//################################################################
//Calculating Fibonacci terms modulo MOD:
//################################################################

struct matrix {
	ULL arr[2][2];
	
	matrix() {
		arr[0][0] = 0;
		arr[0][1] = 0;
		arr[1][0] = 0;
		arr[1][1] = 0;
	}
	
	matrix(ULL a, ULL b, ULL c, ULL d) {
		arr[0][0] = a;
		arr[0][1] = b;
		arr[1][0] = c;
		arr[1][1] = d;
	}
	
	matrix(const matrix& other) {
		arr[0][0] = other.arr[0][0];
		arr[0][1] = other.arr[0][1];
		arr[1][0] = other.arr[1][0];
		arr[1][1] = other.arr[1][1];
	}
	
	void print() {
		printf("  | %llu %llu |\n  | %llu %llu |\n", arr[0][0], arr[0][1], arr[1][0], arr[1][1]);
	}
};

matrix operator+(matrix lhs, const matrix& rhs) {
	matrix res;
	res.arr[0][0] = add(lhs.arr[0][0], rhs.arr[0][0]);
	res.arr[0][1] = add(lhs.arr[0][1], rhs.arr[0][1]);
	res.arr[1][0] = add(lhs.arr[1][0], rhs.arr[1][0]);
	res.arr[1][1] = add(lhs.arr[1][1], rhs.arr[1][1]);
	return res;
}

matrix operator*(const matrix& lhs, const matrix& rhs) {
	matrix res;
	res.arr[0][0] = add(mul(lhs.arr[0][0], rhs.arr[0][0]), mul(lhs.arr[0][1], rhs.arr[1][0]));
	res.arr[0][1] = add(mul(lhs.arr[0][0], rhs.arr[0][1]), mul(lhs.arr[0][1], rhs.arr[1][1]));
	res.arr[1][0] = add(mul(lhs.arr[1][0], rhs.arr[0][0]), mul(lhs.arr[1][1], rhs.arr[1][0]));
	res.arr[1][1] = add(mul(lhs.arr[1][0], rhs.arr[0][1]), mul(lhs.arr[1][1], rhs.arr[1][1]));
	return res;
}

matrix power(matrix m, ULL p) {
	ULL curPow=1;
	matrix res(1,0,0,1);
	
	while (curPow <= p) {
		if ((curPow & p) != 0) {
			res = res * m;
		}
		
		m = m * m;
		curPow <<= 1;
	}
	
	return res;
}


ULL fib(ULL n) {
	matrix m(0, 1, 1, 1);
	matrix answ = power(m, n);
	return answ.arr[1][0];
}


//################################################################
//Finding Fibonacci term with given suffix:
//################################################################

void readInput() {
	char buf[30];
	
	scanf("%s", buf);
	
	sufixSize = strlen(buf);
	
	for (int i=0; i<sufixSize; ++i) {
		sufix[sufixSize-i-1] = (ULL)(buf[i] - '0');
	}
}


bool satisfiesDigit(const ULL fibVal, const int digit) {
	return ((fibVal/pow10[digit])%10) == sufix[digit];
}

//If there exists a Fibonacci number with the lowest 5 digits equal to those requested,
//then there is surely a Fibonacci number, which satisfies the whole request. (Proven empirically :P )
//This implies the existence of a satisfying limit on complexity of this dfs.

pair<bool, ULL> dfs(const ULL fibId, const int digit) {
	if (digit >= sufixSize)
		return make_pair(true, fibId);
	
	ULL candidateId = fibId;
	ULL candidateVal;
	
	for (ULL i=0; i<cycLen[digit]/cycLen[digit-1]; ++i) {
		candidateVal = fib(candidateId);
		
		if (satisfiesDigit(candidateVal, digit)) {
			pair<bool, ULL> res = dfs(candidateId, digit+1);
			if (res.first == true)
				return res;
		}
		
		candidateId += cycLen[digit-1];
	}
	
	return make_pair(false, 0);
}


//################################################################
//Extras for correctness checking:
//################################################################

bool checkIfSatisfies(ULL fibId) {
	if (fibId < 90)
		return false;	//It throws away some correct answers, but ensures the leading zeroes are handled properly
	
	ULL fibVal = fib(fibId);
	
	for (int i=0; i<sufixSize; ++i) {
		if (fibVal%10 != sufix[i])
			return false;
		
		fibVal/=10;
	}
	
	return true;
}


int main() {
	
// 	ULL c;
// 	scanf("%llu", &c);
// 	printf("Fib(%llu) mod %llu = %llu\n", c, MOD, fib(c));
	
	readInput();
	
// 	printf("sufixSize = %d\n", sufixSize);
// 	for (int i=sufixSize-1; i>=0; --i)
// 		printf("sufix[%d] = %llu\n", i, sufix[i]);
// 	printf("\n");
// 	
	
	for (ULL fibId=0; fibId<cycLen[0]; ++fibId) {
		ULL fibVal = fib(fibId);
		
		if (!satisfiesDigit(fibVal, 0))
			continue;
		
		pair<bool, ULL> res = dfs(fibId, 1);
		
		if (res.first == true) {
			#ifndef YESNO_ONLY
				printf("%llu\n", res.second + leadingZeroesFix);
			#endif
			
			#ifdef YESNO_ONLY
				printf("TAK\n");
			#endif
			
// 			printf("final check: %s\n", (checkIfSatisfies(res.second + leadingZeroesFix) ? "correct" : "wrong"));
			return 0;
		}
	}
	
	printf("NIE\n");
	
	return 0;
}