//Autor: Mateusz Wasylkiewicz
//Zawody: Potyczki Algorytmiczne 2015
//Strona: http://potyczki.mimuw.edu.pl/
//Zadanie: Fibonacci, runda 2B
//Czas: Theta(10^n)
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define FOR(x, a, b) for (LL x = (a); x <= (b); x++)
#define REP(x, n) for (LL x = 0; x < (n); x++)
#define SIZE(x) LL((x).size())
const LL MAX = 90;
LL fib[MAX];
void wczytaj_dane(const string& s, LL& n, LL& c)
{
n = SIZE(s);
stringstream ss(s);
ss >> c;
}
void wypelnij_fib()
{
REP(i, 2)
fib[i] = i;
FOR(i, 2, MAX - 1)
fib[i] = fib[i - 1] + fib[i - 2];
}
LL potega(LL a, LL b)
{
LL w = 1;
while (b--)
w *= a;
return w;
}
void rozwiaz(LL n, LL c)
{
LL pot = potega(10, n), a[2] = {0, 1}, g = max(1000LL, (3 * pot) / 2 + 4 * MAX);
REP(k, g)
{
if (a[0] == c && (k >= MAX || fib[k] >= pot / 10))
{
cout << k << '\n';
return;
}
LL pom = (a[0] + a[1]) % pot;
a[0] = a[1];
a[1] = pom;
}
cout << "NIE\n";
}
void zrob_test()
{
string s;
LL n, c;
cin >> s;
wczytaj_dane(s, n, c);
wypelnij_fib();
rozwiaz(n, c);
}
int main()
{
//ios_base::sync_with_stdio(false);
//cin.tie(NULL);
zrob_test();
return 0;
}
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 | //Autor: Mateusz Wasylkiewicz //Zawody: Potyczki Algorytmiczne 2015 //Strona: http://potyczki.mimuw.edu.pl/ //Zadanie: Fibonacci, runda 2B //Czas: Theta(10^n) #include <bits/stdc++.h> using namespace std; typedef long long LL; #define FOR(x, a, b) for (LL x = (a); x <= (b); x++) #define REP(x, n) for (LL x = 0; x < (n); x++) #define SIZE(x) LL((x).size()) const LL MAX = 90; LL fib[MAX]; void wczytaj_dane(const string& s, LL& n, LL& c) { n = SIZE(s); stringstream ss(s); ss >> c; } void wypelnij_fib() { REP(i, 2) fib[i] = i; FOR(i, 2, MAX - 1) fib[i] = fib[i - 1] + fib[i - 2]; } LL potega(LL a, LL b) { LL w = 1; while (b--) w *= a; return w; } void rozwiaz(LL n, LL c) { LL pot = potega(10, n), a[2] = {0, 1}, g = max(1000LL, (3 * pot) / 2 + 4 * MAX); REP(k, g) { if (a[0] == c && (k >= MAX || fib[k] >= pot / 10)) { cout << k << '\n'; return; } LL pom = (a[0] + a[1]) % pot; a[0] = a[1]; a[1] = pom; } cout << "NIE\n"; } void zrob_test() { string s; LL n, c; cin >> s; wczytaj_dane(s, n, c); wypelnij_fib(); rozwiaz(n, c); } int main() { //ios_base::sync_with_stdio(false); //cin.tie(NULL); zrob_test(); return 0; } |
English