-
Notifications
You must be signed in to change notification settings - Fork 126
/
UVa12333.cc
160 lines (142 loc) · 3.72 KB
/
UVa12333.cc
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
// Revenge of Fibonacci, ACM/ICPC Shanghai 2011, UVa12333
// 陈锋
#include<cassert>
#include<iostream>
#include<cstdio>
#include<ctime>
#include<functional>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<sstream>
using namespace std;
const int BASE = 10000;
template<int MaxNode, int Sigma_Size>
struct Trie {
int ch[MaxNode][Sigma_Size], sz, val[MaxNode];
Trie() {
sz = 1;
memset(ch[0], 0, sizeof(ch[0]));
memset(val, 0, sizeof(val));
}
int idx(char c) const { return c - '0'; }
void insert(const string& s, int v) {
assert(v != 0);
int u = 0, n = s.size();
for(int i = 0; i < n; i++) {
int c = idx(s[i]);
if(!ch[u][c]) {
memset(ch[sz], 0, sizeof(ch[sz]));
val[sz] = v;
ch[u][c] = sz++;
}
u = ch[u][c];
}
if(!val[u]) val[u] = v;
// cout<<"insert - "<<s<<",u = "<<u<<",v = "<<val[u]<<endl;
}
int getValue(const string& s) const {
int v = -1, u = 0, n = s.size();
for(int i = 0; i < n; i++) {
int c = idx(s[i]);
if(!ch[u][c]) return v;
u = ch[u][c];
}
// cout<<"getValue "<<s<<",u = "<<u<<",v = "<<val[u]<<endl;
if(val[u]) v = val[u];
return v;
}
};
template<int maxn>
struct bign{
int len, s[maxn];
bign() { memset(s, 0, sizeof(s)); len = 1; }
bign(int num) { *this = num; }
bign& operator=(int num) {
assert(num >= 0);
if(num == 0) { len = 1; return *this; }
len = 0;
while(num > 0) {
// printf("s[%d] = %d\n", len, num%BASE);
s[len++] = num % BASE;
num /= BASE;
}
return *this;
}
bign& operator=(const bign& rhs){
len = rhs.len;
copy(rhs.s, rhs.s + rhs.len, s);
return *this;
}
};
const int MAXF = 100000, MAXLEN = 5300;
typedef bign<MAXLEN> BigFn;
inline void Add(const BigFn& a, const BigFn& b, BigFn& c) {
int *cs = c.s, l = 0;
int mLen = max(a.len, b.len);
for(int i = 0, g = 0; g || i < mLen; i++) {
int x = g;
if(i < a.len) x += a.s[i];
if(i < b.len) x += b.s[i];
cs[l++] = x % BASE;
g = x / BASE;
}
c.len = l;
}
ostream& operator<<(ostream &os, const BigFn& x) {
char buf[8];
stringstream ss;
bool first = true;
for(int i = x.len - 1; i >= 0; i--){
if(first) {
first = false;
ss<<x.s[i];
} else {
sprintf(buf, "%04d", x.s[i]);
ss<<buf;
}
}
const string& s = ss.str();
if(s.empty()) return os<<0;
return os<<s;
}
string getPfx(const BigFn& f, int len = 41) {
int ol = 0;
char buf[8];
stringstream ss;
bool first = true;
for(int i = 0; i < f.len; i++) {
if(first) { first = false; sprintf(buf, "%d", f.s[f.len-i-1]); }
else sprintf(buf, "%04d", f.s[f.len-i-1]);
ss<<buf;
ol += strlen(buf);
if(ol >= len) break;
}
return ss.str();
}
BigFn f0 = 1, f1 = 1, f;
Trie<3817223, 10> pfxes;
int main()
{
for(int i = 2; i < MAXF; i++) {
Add(f0, f1, f);
string pfx = getPfx(f);
pfxes.insert(pfx, i);
f0 = f1;
f1 = f;
}
int T;
scanf("%d", &T);
char buf[64];
for(int t = 1; t <= T; t++) {
scanf("%s", buf);
int ans = 0;
string p(buf);
if(p != "1") ans = pfxes.getValue(p);
printf("Case #%d: %d\n", t, ans);
}
return 0;
}
/* Live Archive AC: 1431647 5711 Revenge of Fibonacci Accepted C++ 3.102 2014-04-11 07:58:13 */