#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const ll INF = 1000000009000000009;
ll rec(vector<int> v)
{
while (!v.empty() && v.back() == 0)
v.pop_back();
if (v.empty())
return 0;
if (sz(v) == 1)
{
ll ans = 0;
if (!(v[0] & 1))
{
for (int i = 0; i < 10; ++i)
{
if ((v[0] & (1 << i)))
ans = (ans * 10 + i);
}
}
else
{
bool z = false;
for (int i = 1; i < 10; ++i)
{
if ((v[0] & (1 << i)))
{
ans = (ans * 10 + i);
if (!z)
{
z = true;
ans = (ans * 10);
}
}
}
if (!z)
ans = 10;
}
return ans;
}
ll ans = INF;
for (int s = 0; s < 10; ++s)
{
int x = s;
vector<int> u = v;
vector<int> nv;
int j = 0;
for (int i = 0; i < sz(u); ++i)
{
u[i] = (u[i] & (((1 << 10) - 1) ^ (1 << x)));
if (x == 9)
{
int y = 0;
while (j <= i)
y |= u[j++];
nv.push_back(y);
}
x = (x + 1) % 10;
}
{
int y = 0;
while (j < sz(u))
y |= u[j++];
if (y)
nv.push_back(y);
}
if (sz(v) == 2 && sz(nv) == 2 && v[0] == nv[0] && v[1] == nv[1])
continue;
ll yans = rec(nv);
if (s == 0 && yans == 0)
yans = 1;
ans = min(ans, yans * 10 + s);
}
{
int s = 0;
int x = s;
vector<int> u = v;
vector<int> nv;
int j = 0;
for (int i = 0; i < sz(u); ++i)
{
if (i > 0)
u[i] = (u[i] & (((1 << 10) - 1) ^ (1 << x)));
if (x == 9)
{
int y = 0;
while (j <= i)
y |= u[j++];
nv.push_back(y);
}
x = (x + 1) % 10;
}
{
int y = 0;
while (j < sz(u))
y |= u[j++];
if (y)
nv.push_back(y);
}
if (!(sz(v) == 2 && sz(nv) == 2 && v[0] == nv[0] && v[1] == nv[1]))
{
ll yans = rec(nv);
ans = min(ans, yans * 10 + s);
}
}
return ans;
}
bool stg(ll ans, vector<int> v)
{
bool z = true;
int x = ans;
for (int i = 0; i < sz(v); ++i)
{
bool zz = false;
int y = x;
while (y)
{
if ((1 << y % 10) == v[i])
{
zz = true;
break;
}
y /= 10;
}
if (!zz)
{
z = false;
break;
}
++x;
}
return z;
}
void solv()
{
int n;
cin >> n;
vector<int> v;
while (n--)
{
int x;
cin >> x;
v.push_back((1 << x));
}
ll ans = rec(v);
if (!ans)
{
ans = 1;
while (1)
{
if (stg(ans, v))
break;
ans *= 10;
}
}
cout << ans << "\n";
}
int main()
{
#ifdef SOMETHING
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif // SOMETHING
ios_base::sync_with_stdio(false), cin.tie(0);
/*for (int l = 1; l <= 1000; ++l)
{
for (int r = l; r <= l + 20; ++r)
{
vector<int> v;
for (int i = l; i <= r; ++i)
{
int x = i;
vector<int> vx;
while (x)
{
vx.push_back(x % 10);
x /= 10;
}
v.push_back((1 << vx[rnf() % sz(vx)]));
}
ll ans = rec(v);
if (!ans)
{
ans = 1;
while (1)
{
if (stg(ans, v))
break;
ans *= 10;
}
}
if (ans > l)
{
cout << "WA0\n";
cout << l << ' ' << r << "\n";
cout << ans << "\n";
rec(v);
return 0;
}
else if (!stg(ans, v))
{
cout << "WA1\n";
cout << l << ' ' << r << "\n";
cout << ans << "\n";
rec(v);
return 0;
}
}
}
cout << "OK\n";
return 0;*/
int tt = 1;
//cin >> tt;
while (tt--)
solv();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
224 KB |
Output is correct |
8 |
Correct |
3 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
4 ms |
332 KB |
Output is correct |
11 |
Correct |
4 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
6 ms |
332 KB |
Output is correct |
15 |
Correct |
6 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
3 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
6 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
3 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
3 ms |
332 KB |
Output is correct |
12 |
Correct |
3 ms |
332 KB |
Output is correct |
13 |
Correct |
4 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
324 KB |
Output is correct |
16 |
Correct |
6 ms |
332 KB |
Output is correct |
17 |
Correct |
7 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
324 KB |
Output is correct |
19 |
Correct |
2 ms |
332 KB |
Output is correct |
20 |
Correct |
4 ms |
332 KB |
Output is correct |
21 |
Correct |
3 ms |
332 KB |
Output is correct |
22 |
Correct |
5 ms |
328 KB |
Output is correct |
23 |
Correct |
4 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
47 ms |
488 KB |
Output is correct |
3 |
Correct |
43 ms |
484 KB |
Output is correct |
4 |
Correct |
36 ms |
460 KB |
Output is correct |
5 |
Correct |
33 ms |
452 KB |
Output is correct |
6 |
Correct |
20 ms |
460 KB |
Output is correct |
7 |
Correct |
172 ms |
1424 KB |
Output is correct |
8 |
Correct |
168 ms |
1096 KB |
Output is correct |
9 |
Correct |
220 ms |
1872 KB |
Output is correct |
10 |
Correct |
256 ms |
1868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
208 ms |
976 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
4 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
10 |
Correct |
3 ms |
332 KB |
Output is correct |
11 |
Correct |
249 ms |
1884 KB |
Output is correct |
12 |
Correct |
438 ms |
1868 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
3 ms |
332 KB |
Output is correct |
15 |
Correct |
5 ms |
332 KB |
Output is correct |
16 |
Correct |
4 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
6 ms |
340 KB |
Output is correct |
20 |
Correct |
4 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
332 KB |
Output is correct |
23 |
Correct |
4 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
25 |
Correct |
4 ms |
332 KB |
Output is correct |
26 |
Correct |
5 ms |
332 KB |
Output is correct |
27 |
Correct |
46 ms |
460 KB |
Output is correct |
28 |
Correct |
44 ms |
488 KB |
Output is correct |
29 |
Correct |
24 ms |
460 KB |
Output is correct |
30 |
Correct |
24 ms |
460 KB |
Output is correct |
31 |
Correct |
16 ms |
476 KB |
Output is correct |
32 |
Correct |
163 ms |
1356 KB |
Output is correct |
33 |
Correct |
179 ms |
1104 KB |
Output is correct |
34 |
Correct |
265 ms |
1884 KB |
Output is correct |
35 |
Correct |
246 ms |
1876 KB |
Output is correct |
36 |
Correct |
336 ms |
1448 KB |
Output is correct |
37 |
Correct |
479 ms |
1860 KB |
Output is correct |
38 |
Correct |
271 ms |
1232 KB |
Output is correct |
39 |
Correct |
520 ms |
1868 KB |
Output is correct |
40 |
Correct |
518 ms |
1868 KB |
Output is correct |