-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path457-scenes-from-a-memory.cpp
More file actions
273 lines (267 loc) · 5.53 KB
/
Copy path457-scenes-from-a-memory.cpp
File metadata and controls
273 lines (267 loc) · 5.53 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* Scenes From a Memory [1562B]
* Problem: https://codeforces.com/problemset/problem/1562/B
* Verdict: ACCEPTED Solved: 2021-08-26
* Language: C++17 (GCC 7-32)
* Runtime: 31 ms Memory: 3800 KB
* Tags: brute force, constructive algorithms, implementation, math, number theory
* Author: BidoTeima
* Source: https://codeforces.com/contest/1562/submission/127128738
*/
/// isA AC
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("-Ofast")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int64_t, null_type,less<int64_t>, rb_tree_tag,tree_order_statistics_node_update>
#define ordered_multiset tree<int64_t, null_type,less_equal<int64_t>, rb_tree_tag,tree_order_statistics_node_update>
// Important functions and defines
/// BidoTeima legacy functions
using ll = long long;
ll mod = (ll)1e9+7;
ll MOD = mod;
int64_t POW(int64_t a, int64_t b)
{
if(b==0) return 1;
a%=mod;
int64_t res = POW(a,b/2);
if(b&1)
return (res%mod*res%mod*a%mod)%mod;
return (res%mod*res%mod)%mod;
}
int64_t POWNOM(int64_t a, int64_t b)
{
if(b==0) return 1;
int64_t res = POWNOM(a,b/2);
if(b&1)
return (res*res*a);
return (res*res);
}
ll sum_range2d(ll i, ll j, ll k, ll l, vector<vector<ll>>& vec)
{
return vec[k][l] - vec[k][j - 1] - vec[i - 1][l] + vec[i - 1][j - 1];
}
ll GCDAC(ll a, ll b)
{
if(b==0)
return a;
return GCDAC(b, a%b);
}
ll gcd(ll a, ll b)
{
if(a<b)swap(a,b);
return GCDAC(a, b);
}
ll lcm(ll a,ll b)
{
return (a/gcd(a,b))*b;
}
double dist(double X1, double Y1, double X2, double Y2)
{
return sqrt( ( (X1 - X2)*(X1 - X2) ) + ( (Y1-Y2)*(Y1-Y2) ) );
}
bool intersect(pair<ll,ll>p1, pair<ll,ll>p2)
{
ll x1=p1.first,x2=p1.second,y1=p2.first,y2=p2.second;
return (x1 >= y1 && x1 <= y2) ||
(x2 >= y1 && x2 <= y2) ||
(y1 >= x1 && y1 <= x2) ||
(y2 >= x1 && y2 <= x2);
}
bool isPrime(ll n)
{
if(n==2)
return true;
if(n%2==0||n==1)
return false;
for (ll i = 3; i * i <= n; i+=2)
if (n % i == 0)
return false;
return true;
}
ll inverse(ll n)
{
return POW(n%mod,mod-2);
}
template<typename T1, typename T2>
bool mul_overflow(T1 x, T2 y) { // log(xy)=log(x)+log(y)
if(x==0||y==0)
return 0;
return (log2(x)+log2(y)) >= 64.0;
}
ll sod(ll x){
ll ret=0;
while(x){
ret+=x%10;
x/=10;
}
return ret;
}
ll ceili(ll x,ll y){
return (x + y - 1) / y;
}
string operator*(const string& a, size_t n){
string ret;
for(int i = 0; i < n; i++)ret+=a;
return ret;
}
void operator*=(string& a, size_t n){
string cpy=a;
for(int i = 1; i < n; i++)a+=cpy;
if(!n)a="";
}
void append(vector<int>&a,const vector<int>&b){
for(auto&i:b)a.push_back(i);
}
bool isPalindrome(const string& s){
bool ok = 1;
for(int i = 0; i <= (int)s.size()/2; i++){
ok&=(s[i]==s[(int)s.size()-i-1]);
}
return ok;
}
bool isPOWB(int64_t a, int64_t b, ll lm){
int64_t cur=1;
for(ll i = 0; i <= lm; i++){
if(cur==a)return true;
if(i<lm)cur*=b;
}
return false;
}
void ACPLS(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
ios::sync_with_stdio(0);cout.tie(0);cin.tie(0);
}
vector<vector<ll>> C;
void bin_coeff(ll n, ll k)
{
C=vector<vector<ll>>(n+3,vector<ll>(k+3,0));
for(int i = 0; i <= min(n,k); i++) C[i][i]=1, C[i][0]=1;
for(int i = 0; i <= max(n,k); i++) C[i][0]=1;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= k; j++) {
C[i][j] = C[i-1][j] + C[i-1][j-1];
}
}
}
vector<int> prime;
void sievePrime(ll n)
{
prime.resize(n+3,1);
prime[0]=false;
prime[1]=false;
for (ll p=2; p*p<=n; p++)
{
if (prime[p] == true)
{
for (ll i=p*p; i<=n; i += p)
prime[i] = false;
}
}
}
template<typename _Cont_Ty>
void pa(_Cont_Ty Cont, bool space = 1){
for(auto it = std::begin(Cont); it != std::end(Cont); ++it){
cout<<*it;
if(space)cout<<' ';
}
cout<<'\n';
}
template<typename _It_Ty>
void pa(_It_Ty beg, _It_Ty en, bool space = 1){
for(auto it = beg; it != en; ++it){
cout<<*it;
if(space)cout<<' ';
}
cout<<'\n';
}
template<typename _Ty>
_Ty& minref(_Ty& a, _Ty& b){
if(a<b)return a;
return b;
}
/// End of BidoTeima legacy functions
#define ALL(x) (x).begin(),(x).end()
#define all(cont) (cont).begin(), (cont).end()
#define rall(cont) (cont).rbegin(), (cont).rend()
#define tc int tt;cin>>tt;while(tt--)
#define sumrange1d(l,r,pref) (l==0?pref[r]:pref[r]-pref[l-1])
int main()
{
ACPLS();
tc{
int k;
cin>>k;
string s;
cin>>s;
ll ans=1e11;
if(k<=9){
for(int i = 1; i < (1<<k); i++){
ll num=0;
for(int j = 0; j < k; j++){
if(i&(1<<j)){
num*=10;
num+=s[j]-'0';
}
}
if(!isPrime(num)){
if(log10(num)<log10(ans))
ans=num;
}
}
cout<<int(log10(ans))+1<<'\n';
cout<<ans<<'\n';
continue;
}
int freq[10]={0};
for(auto&i:s)freq[i-'0']++;
for(int i = 1; i < 10; i++){
if(isPrime(i))continue;
if(freq[i]>0)ans=i;
}
if(ans!=1e11){
cout<<1<<'\n'<<ans<<'\n';
continue;
}
for(int i = 2; i < 10; i++){
if(freq[i]>1){
ans=i;
break;
}
}
if(ans!=1e11){
cout<<2<<'\n';
cout<<ans<<ans<<'\n';
continue;
}
bool found2=0;
bool foundans=0;
for(int i = k; i >= 0; i--){
if(s[i]==2&&found2==0){
found2=1;
}
else if(found2){
cout<<2<<'\n';
cout<<s[i]<<2<<'\n';
foundans=1;
break;
}
}
if(foundans){
continue;
}
for(int i = 1; i < 10; i++){
if(freq[i]>2){
ans=i;
break;
}
}
cout<<3<<'\n';
cout<<ans<<ans<<ans<<'\n';
}
}