#include <cstdio> #include <cstdlib> #include <vector> #include <set> #include <map> #include <string> #include <algorithm> #include <iostream> #include <sstream> using namespace std; typedef unsigned long long LL; #define MAX(a,b) ((a) > (b) ? (a) : (b)) #define MIN(a,b) ((a) < (b) ? (a) : (b)) #define MP make_pair #define ST first #define ND second #define PB push_back #define FOR(i,b,e) for(int i=(b); i<=(e); ++i) #define FORD(i,b,e) for(int i=(b); i>=(e); --i) #define REP(i,n) for(int i=0; i<n; ++i) #define VAR(v,n) __typeof(n) v = (n) #define SIZE(x) ((int)(x).size()) #define FOREACH(i,x) for(VAR(i,(x).begin()); i!=(x).end(); ++i) #define SC scanf #define PR printf #define MAXN 19 class Big{ public: int n[MAXN]; Big () {REP(i,MAXN) n[i]=0;}; Big (string s) { REP(i,MAXN) n[i]=0; FORD(i,s.size()-1,0) n[i]=s[s.size()-i-1]-48; } Big operator +(Big p) { Big b; REP(i,MAXN) b.n[i]=n[i]+p.n[i]; REP(i,MAXN-1) { if(b.n[i]>=10){ b.n[i+1]++; b.n[i]-=10; } } if(b.n[MAXN-1]>=10){ b.n[MAXN-1]-=10; } return b; } Big operator *(Big p) { Big b; REP(i,MAXN){ REP(j,MAXN-i){ b.n[i+j]+=n[i]*p.n[j]; } } REP(i,MAXN-1){ b.n[i+1]+=b.n[i]/10; b.n[i]%=10; } b.n[MAXN-1]%=10; return b; } void print(){ FORD(i,MAXN-1,0) printf("%d", n[i]); printf("\n"); } }; Big fib(int n){ int i = n - 1; Big a,b,c,d,t; a.n[0] = 1; b.n[0] = 0; d.n[0] = 1; if (n <= 0) return t; while (i > 0){ while (i % 2 == 0){ t = d*(c+c + d); c = c*c + d*d; d = t; i>>=1; } t = d*(b + a) + c*b; a = d*b + c*a; b = t; i--; } return a + b; } int find_last_two(int x, int m){ int a=0, b=1, i=1,t; while(b!=x && i<15000){ t=a; a=b; b=t+a; if(b>=m) b-=m; ++i; } if(b==x) return i; else return -1; } int main() { string str; cin >> str; Big x(str); int cyf=str.size(); LL p = -1; if(cyf==1){ p = find_last_two(x.n[0], 10); if(p==-1) printf("NIE\n"); else printf("%lld", p+1500); return 0; } else if(cyf==2){ p = find_last_two(x.n[0]+10*x.n[1], 100); if(p==-1) printf("NIE\n"); else printf("%lld", p+1500); return 0; } //x.print(); p = find_last_two(x.n[0]+10*x.n[1]+100*x.n[2], 1000); if(p==-1){ printf("NIE\n"); return 0; } //printf("%d\n", cyf); LL dod = 150; FOR(i,2,cyf-1) { Big b = fib(p); //printf("%d %d %d\n", i, x.n[i], b.n[i]); //b.print(); int j=0; while(b.n[i]!=x.n[i] && j<=10) { p+=dod; b=fib(p); //printf("%d ", b.n[i]); b.print(); j++; } if(b.n[i]!=x.n[i] && j==10){ printf("NIE\n"); return 0; } dod*=10; } printf("%lld\n", p+dod); 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 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 | #include <cstdio> #include <cstdlib> #include <vector> #include <set> #include <map> #include <string> #include <algorithm> #include <iostream> #include <sstream> using namespace std; typedef unsigned long long LL; #define MAX(a,b) ((a) > (b) ? (a) : (b)) #define MIN(a,b) ((a) < (b) ? (a) : (b)) #define MP make_pair #define ST first #define ND second #define PB push_back #define FOR(i,b,e) for(int i=(b); i<=(e); ++i) #define FORD(i,b,e) for(int i=(b); i>=(e); --i) #define REP(i,n) for(int i=0; i<n; ++i) #define VAR(v,n) __typeof(n) v = (n) #define SIZE(x) ((int)(x).size()) #define FOREACH(i,x) for(VAR(i,(x).begin()); i!=(x).end(); ++i) #define SC scanf #define PR printf #define MAXN 19 class Big{ public: int n[MAXN]; Big () {REP(i,MAXN) n[i]=0;}; Big (string s) { REP(i,MAXN) n[i]=0; FORD(i,s.size()-1,0) n[i]=s[s.size()-i-1]-48; } Big operator +(Big p) { Big b; REP(i,MAXN) b.n[i]=n[i]+p.n[i]; REP(i,MAXN-1) { if(b.n[i]>=10){ b.n[i+1]++; b.n[i]-=10; } } if(b.n[MAXN-1]>=10){ b.n[MAXN-1]-=10; } return b; } Big operator *(Big p) { Big b; REP(i,MAXN){ REP(j,MAXN-i){ b.n[i+j]+=n[i]*p.n[j]; } } REP(i,MAXN-1){ b.n[i+1]+=b.n[i]/10; b.n[i]%=10; } b.n[MAXN-1]%=10; return b; } void print(){ FORD(i,MAXN-1,0) printf("%d", n[i]); printf("\n"); } }; Big fib(int n){ int i = n - 1; Big a,b,c,d,t; a.n[0] = 1; b.n[0] = 0; d.n[0] = 1; if (n <= 0) return t; while (i > 0){ while (i % 2 == 0){ t = d*(c+c + d); c = c*c + d*d; d = t; i>>=1; } t = d*(b + a) + c*b; a = d*b + c*a; b = t; i--; } return a + b; } int find_last_two(int x, int m){ int a=0, b=1, i=1,t; while(b!=x && i<15000){ t=a; a=b; b=t+a; if(b>=m) b-=m; ++i; } if(b==x) return i; else return -1; } int main() { string str; cin >> str; Big x(str); int cyf=str.size(); LL p = -1; if(cyf==1){ p = find_last_two(x.n[0], 10); if(p==-1) printf("NIE\n"); else printf("%lld", p+1500); return 0; } else if(cyf==2){ p = find_last_two(x.n[0]+10*x.n[1], 100); if(p==-1) printf("NIE\n"); else printf("%lld", p+1500); return 0; } //x.print(); p = find_last_two(x.n[0]+10*x.n[1]+100*x.n[2], 1000); if(p==-1){ printf("NIE\n"); return 0; } //printf("%d\n", cyf); LL dod = 150; FOR(i,2,cyf-1) { Big b = fib(p); //printf("%d %d %d\n", i, x.n[i], b.n[i]); //b.print(); int j=0; while(b.n[i]!=x.n[i] && j<=10) { p+=dod; b=fib(p); //printf("%d ", b.n[i]); b.print(); j++; } if(b.n[i]!=x.n[i] && j==10){ printf("NIE\n"); return 0; } dod*=10; } printf("%lld\n", p+dod); return 0; } |