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 xxis the order number of the test case and yy is the answer.
样例输入
2 6 100
样例输出
Case #1: 5 Case #2: 73
题目来源
题意:一个supreme数是其的所有子集都是质数或者1(可以是不连续的子集),求小于n的最大的supreme数
~一言难尽,当时以为连续呢,(Q_Q)
思路:考虑单独的一个数是supreme数或1的有1,2,3,5,7,二位数是supreme数的有73,71,53,37,31,23,17,13,11,三位数是supreme数的有317,311,173,137,131,113,四位数没有supreme数
代码:
#include <bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
char s[1100];
int a[20]={1,2,3,5,7,11,13,17,23,31,37,53,71,73,113,131,137,173,311,317};
int main()
{
ll t,cas=1;
scanf("%lld",&t);
while(t--)
{
scanf("%s",s);
int l=strlen(s);
printf("Case #%lld: ",cas++);
if(l>=4)
printf("317\n");
else
{
int f=0,i=0;
while(s[i])
f=f*10+(s[i]-'0'),i++;
for(i=19;i>=0;i--)
{
if(a[i]<=f)
{
printf("%d\n",a[i]);
break;
}
}
}
}
return 0;
}