B. Scenes From a Memory
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
During the hypnosis session, Nicholas suddenly remembered a positive integer nn, which doesn't contain zeros in decimal notation.
Soon, when he returned home, he got curious: what is the maximum number of digits that can be removed from the number so that the number becomes not prime, that is, either composite or equal to one?
For some numbers doing so is impossible: for example, for number 5353 it's impossible to delete some of its digits to obtain a not prime integer. However, for all nn in the test cases of this problem, it's guaranteed that it's possible to delete some of their digits to obtain a not prime number.
Note that you cannot remove all the digits from the number.
A prime number is a number that has no divisors except one and itself. A composite is a number that has more than two divisors. 11 is neither a prime nor a composite number.
Input
Each test contains multiple test cases.
The first line contains one positive integer tt (1≤t≤1031≤t≤103), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains one positive integer kk (1≤k≤501≤k≤50) — the number of digits in the number.
The second line of each test case contains a positive integer nn, which doesn't contain zeros in decimal notation (10k−1≤n<10k10k−1≤n<10k). It is guaranteed that it is always possible to remove less than kk digits to make the number not prime.
It is guaranteed that the sum of kk over all test cases does not exceed 104104.
Output
For every test case, print two numbers in two lines. In the first line print the number of digits, that you have left in the number. In the second line print the digits left after all delitions.
If there are multiple solutions, print any.
Example
input
Copy
7 3 237 5 44444 3 221 2 35 3 773 1 4 30 626221626221626221626221626221
output
Copy
2 27 1 4 1 1 2 35 2 77 1 4 1 6
Note
In the first test case, you can't delete 22 digits from the number 237237, as all the numbers 22, 33, and 77 are prime. However, you can delete 11 digit, obtaining a number 27=3327=33.
In the second test case, you can delete all digits except one, as 4=224=22 is a composite number.
如果我用素数筛法的 st[ ] 数组来区分 非素数,
但是非素数 不仅包括合数 也包括 1 这个不仅是非素数也是非合数的。
-----------------------------------------------------------------------------------------------------------
由于st[ ]=true 表示的是 合数
所以我们要手动 把 st[1]=true
------------------------------------------------------------------------------------------------------
或者换一种方法,用素数判定函数,来筛选 非素数 (包括合数和1)
#include <bits/stdc++.h>
using namespace std;
const int N=20;
bool st[N][N];
bool st1[110];
int cnt=0,primes[110];
void get_primes(int n)
{
st1[1]=true;
for(int i=2;i<=n;i++)
{
if(!st1[i])
{
primes[cnt++]=i;
}
for(int j=0;primes[j]<=n/i;j++)
{
st1[primes[j]*i]=true;
if(i%primes[j]==0) break;
}
}
}
int main()
{
get_primes(109);
for(int i=10;i<=99;i++)
{
int b=i%10;
int a=i/10%10;
if(st1[i])
st[a][b]=true;
}
int t;
cin>>t;
while(t--)
{
int k;
string s;
cin>>k;
cin>>s;
int p=-1;
for(int i=0;i<s.size();i++)
{
int t=s[i]-'0';
if(st1[t])
{
p=t;
break;
}
}
if(p!=-1)
{
//cout<<"da an"<<endl;
cout<<"1"<<endl;
cout<<p<<endl;
//continue;
}
else if(p==-1)
{
bool flag=false;
string res;
for(int i=0;i<s.size();i++)
{
res.clear();
for(int j=i+1;j<s.size();j++)
{
int a=s[i]-'0';
int b=s[j]-'0';
if(st[a][b])
{
res+=s[i];
res+=s[j];
flag=true;
break;
}
}
if(flag) break;
}
//cout<<"da an"<<endl;
cout<<"2"<<endl;
cout<<res<<endl;
}
}
return 0;
}