forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10909.cpp
180 lines (168 loc) · 2.68 KB
/
10909.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
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
173
174
175
176
177
178
179
180
#include <bits/stdc++.h>
using namespace std;
#define MAXN 670000
int left_[MAXN], right_[MAXN], parent[MAXN], key[MAXN], cnt[MAXN], N, root;
char lucky[2010000];
// idx of k-th smallest elem
int find(int k)
{
for (int x = root;;)
{
if (k < cnt[left_[x]])
{
x = left_[x];
}
else if (k == cnt[left_[x]])
{
return x;
}
else
{
k -= cnt[left_[x]] + 1, x = right_[x];
}
}
return -1;
}
// rm elem(idx=x)
void rm(int x)
{
int y;
if (left_[x] != 0 && right_[x] != 0)
{
if (cnt[right_[x]] >= cnt[left_[x]])
{
for (y = right_[x]; left_[y] != 0; y = left_[y])
;
}
else
{
for (y = left_[x]; right_[y] != 0; y = right_[y])
;
}
key[x] = key[y];
x = y;
}
if (left_[x] == 0 && right_[x] == 0)
{
if (left_[parent[x]] == x)
{
left_[parent[x]] = 0;
}
else
{
right_[parent[x]] = 0;
}
}
else
{
y = (left_[x] == 0) ? right_[x] : left_[x];
if (parent[x] == 0)
{
parent[root = y] = 0;
return;
}
if (left_[parent[x]] == x)
{
left_[parent[x]] = y;
}
else
{
right_[parent[x]] = y;
}
parent[y] = parent[x];
}
for (x = parent[x]; x != 0; x = parent[x])
{
cnt[x]--;
}
}
// construct balanced tree with b-a+1 elements; returns idx@root
// tree's nodes will get consecutive idxs in tree order
int build(int a, int b)
{
if (a > b)
{
return 0;
}
if (a == b)
{
N++;
left_[N] = right_[N] = 0;
cnt[N] = 1;
return N;
}
int c = (a + b) / 2, t = build(a, c - 1);
left_[++N] = t;
t = N;
right_[t] = build(c + 1, b);
cnt[t] = cnt[left_[t]] + cnt[right_[t]] + 1;
parent[left_[t]] = parent[right_[t]] = t;
return t;
}
void mark(int x)
{
for (; x; x = right_[x])
{
lucky[key[x]] = 1, mark(left_[x]);
}
}
// construct lst of lucky #
void make()
{
int i, j, k;
// init
N = cnt[0] = 0;
parent[root = build(0, 666667)] = 0;
// start with tree, containing all #
// of form 6k+1 and 6k+3, in range of interest
// these are remaining # after 1st 2 elimination rds
for (i = 1, j = 1; i <= 666700; j += 6)
{
key[i++] = j, key[i++] = j + 2;
}
// simulate
for (k = 2; k < cnt[root]; k++)
{
j = key[find(k)] - 1;
if (j >= cnt[root])
{
break;
}
for (i = j; i < cnt[root]; i += j)
{
rm(find(i));
}
}
// mark remaining # in lucky[]
memset(lucky, 0, sizeof(lucky));
mark(root);
}
int main()
{
int a, n;
for (make(); scanf("%d", &n) == 1;)
{
a = 0;
if (n >= 1 && (n & 1) == 0)
{
for (a = n / 2; a > 0 && !lucky[a]; a--)
;
for (; a > 0; a -= 2)
{
if (lucky[a] && lucky[n - a])
{
break;
}
}
}
if (a <= 0)
{
printf("%d is not the sum of two luckies!\n", n);
}
else
{
printf("%d is the sum of %d and %d.\n", n, a, n - a);
}
}
return 0;
}