-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10183.cpp
116 lines (102 loc) · 2.83 KB
/
10183.cpp
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
#include <cmath>
#include <cstdio>
#include <cstring>
#define BASE 1000000000000
using namespace std;
struct fab {
int len;
long digit[10];
};
static inline int cmp(fab *x, fab *y)
{
int i;
if (x->len == y->len) {
for (i = x->len - 1; i > -1; --i)
if (x->digit[i] > y->digit[i])
return 1;
else if (x->digit[i] < y->digit[i])
return -1;
}
else
return x->len - y->len;
return 0;
}
int main(void)
{
char bn[110], en[110];
fab list[500], bound, beg, end;
int len, begI, endI, i, j, con, map[128];
for (i = 0; i < 10; ++i)
map[i + '0'] = i;
memset(list[1].digit, 0, sizeof(long) * 10);
list[1].len = 1;
list[1].digit[0] = 1;
memset(list[2].digit, 0, sizeof(long) * 10);
list[2].len = 1;
list[2].digit[0] = 1;
memset(bound.digit, 0, sizeof(long) * 10);
bound.len = 9;
bound.digit[8] = 10000;
len = 3;
while (true) {
memcpy(list + len, list + len - 1, sizeof(fab));
for (i = 0; list[len].digit[i]; ++i) {
list[len].digit[i] += list[len - 2].digit[i];
if (list[len].digit[i] >= BASE) {
list[len].digit[i + 1]++;
list[len].digit[i] -= BASE;
}
}
list[len].len = i;
if (cmp(list + len, &bound) >= 0)
break;
else
len++;
}
len++;
while (true) {
scanf("%s %s", bn, en);
if (bn[0] == '0' && en[0] == '0' && strlen(bn) == 1 && strlen(en) == 1) break;
memset(beg.digit, 0, sizeof(long) * 10);
memset(end.digit, 0, sizeof(long) * 10);
begI = strlen(bn);
beg.len = --begI / 12 + 1;
for (i = 0; i <= begI; ++i)
beg.digit[(begI - i) / 12] += (long)map[bn[i]] * (long)pow(10, (begI - i) % 12);
endI = strlen(en);
end.len = --endI / 12 + 1;
for (i = 0; i <= endI; ++i)
end.digit[(endI - i) / 12] += (long)map[en[i]] * (long)pow(10, (endI - i) % 12);
i = 1;
j = len;
while (j - i > 1) {
begI = (i + j) / 2;
con = cmp(&beg, list + begI);
if (con < 0)
j = begI;
else if (con > 0)
i = begI;
else
break;
}
i = 1;
j = len;
while (j - i > 1) {
endI = (i + j) / 2;
con = cmp(&end, list + endI);
if (con < 0)
j = endI;
else if (con > 0)
i = endI;
else
break;
}
if (cmp(&beg, list + begI) > 0) begI++;
if (cmp(&end, list + endI) < 0) endI--;
if (begI <= endI)
printf("%d\n", endI - begI + 1);
else
printf("0\n");
}
return 0;
}