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
#include <cstdio>
#include <cstdlib>

#define MUL_SUM_MOD(a, b, c, d, m) ((mulmod(a, b, m) + mulmod(c, d, m))%(m))
#define MAX_NO_OF_DIGITS 18

using namespace std;

typedef long long int lli;
typedef unsigned long long int ull;

lli searches_digits;
lli modulo_y;
int no_of_digits;

ull mulmod(ull a,ull b,ull c)
{
    if (a<=1000000000ULL && b<=1000000000ULL)
    {
        ull ret = ((a%c)*(b%c))%c;
        return ret;
    }

    ull ret = 0ULL;
    a = a%c;

    while (b > 0ULL)
    {
        if(b&1ULL)
        {
            ret = (ret+a)%c;
        }
        a = (a<<1ULL)%c;
        b>>=1ULL;
    }

    return ret%c;
}

void multiply(ull F[2][2], ull M[2][2], ull modulo_x)
{
	ull x =  MUL_SUM_MOD(F[0][0], M[0][0], F[0][1], M[1][0], modulo_x);
	ull y =  MUL_SUM_MOD(F[0][0], M[0][1], F[0][1], M[1][1], modulo_x);
	ull z =  MUL_SUM_MOD(F[1][0], M[0][0], F[1][1], M[1][0], modulo_x);
	ull w =  MUL_SUM_MOD(F[1][0], M[0][1], F[1][1], M[1][1], modulo_x);

    F[0][0] = x;
    F[0][1] = y;
    F[1][0] = z;
    F[1][1] = w;
}

void power(ull F[2][2], ull n, ull modulo_x)
{
    if( n == 0 || n == 1)
        return;
    ull M[2][2] = {{1,1},{1,0}};

    power(F, n/2, modulo_x);
    multiply(F, F, modulo_x);

    if(n%2 != 0)
        multiply(F, M, modulo_x);
}

ull fib(ull n, ull modulo_x)
{
	ull F[2][2] = {{1,1},{1,0}};
    if (n == 0)
        return 0;
    power(F, n-1, modulo_x);
    return F[0][0];
}

lli step(int i)
{
    if(i==0)
        return 1;
    if(i==1)
        return 60;
    if(i==2)
        return 300;
    lli a=1500;
    for(int j=3; j<i; j++)
        a*=10;
    return a;
}

int ends( lli i ,int pos)
{
    lli dec=10;
    for( int k=1; k<pos; k++)
        dec*=10;
    if( (i%dec) == (searches_digits%dec) )
        return 1;
    else
        return 0;
}

bool check( int position, lli parent_num )
{
    lli inc = step(position-1);
    lli range = step(position) + parent_num;
    for(lli i = parent_num; i<range; i+=inc)
    {
        lli my_fib = fib(i, modulo_y);
        if ( ends(my_fib, position) )
        {
            if( position <= no_of_digits )
            	check(position+1, i);
            else
            {
                printf("%llu\n", i);
                exit(0);
            }
        }
    }
    return false;
}

int main()
{
    if( scanf("%lld", &searches_digits) )
    {
        if(searches_digits == 0)
        {
            printf("0");
            return 0;
        }
        modulo_y = 10;
        for(no_of_digits = 1; no_of_digits < MAX_NO_OF_DIGITS; no_of_digits++)
        {
    	    modulo_y*=10;
            if( modulo_y > searches_digits )
                break;
        }
        check( 1 , 1 );
        printf("NIE");
    }
    return 0;
}