A prime number (or a prime) is a natural number greater than 11 that cannot be formed by multiplying two smaller natural numbers.
Now lets define a number NN as the supreme number if and only if each number made up of an non-empty subsequence of all the numeric digits of NN must be either a prime number or 11.
For example, 1717 is a supreme number because 11, 77, 1717 are all prime numbers or 11, and 1919 is not, because 99 is not a prime number.
Now you are given an integer N\ (2 \leq N \leq 10^{100})N (2≤N≤10100), could you find the maximal supreme number that does not exceed NN?
Input
In the first line, there is an integer T\ (T \leq 100000)T (T≤100000) indicating the numbers of test cases.
In the following TT lines, there is an integer N\ (2 \leq N \leq 10^{100})N (2≤N≤10100).
Output
For each test case print "Case #x: y"
, in which xx is the order number of the test case and yy is the answer.
样例输入
2
6
100
样例输出
Case #1: 5
Case #2: 73
输出小于等于n的最大supreme number,superme number:该串所有的子序列都为质数。
注意:子序列(subsequence)可以不连续;子串(substring)必须连续。
思路:找规律,发现符合要求的数并不多,打表。
#include<bits/stdc++.h>
using namespace std;
int num[20]={317,311,173,137,131,113,73,71,53,37,31,23,17,13,11,7,5,3,2,1};
int main()
{
int T,cas=1;
cin>>T;
while(T--)
{
string s;
cin>>s;
int n=s.length();
cout<<"Case #"<<cas++<<": ";
if(n>=4)
cout<<"317"<<endl;
else
{
int t=0;
for(int i=0;i<n;i++)
t=t*10+(s[i]-'0');
for(int i=0;i<20;i++)
{
if(t>=num[i])
{
cout<<num[i]<<endl;
break;
}
}
}
}
return 0;
}