ACM-ICPC 2018 沈阳赛区网络预赛 K-Supreme Number

Supreme Number   

 题库链接

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

题目来源

ACM-ICPC 2018 沈阳赛区网络预赛

因为即使非连续时形成了非质数也不行,因此情况非常有限,所以自己手动枚举一下就可以了

AC代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<sstream>
#include<algorithm>
using namespace std;
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()
{
	int t;
	string s;
	scanf("%d",&t);
	for(int u = 1;u <= t;++u)
	{
		cin>>s;
		if(s.size() >= 4)
			printf("Case #%d: 317\n",u);
		else
		{
			stringstream ss(s);
			int k;
			ss >> k;
			int x=upper_bound(a,a+20,k)-a;
			if (x) x--;
			printf("Case #%d: %d\n",u,a[x]);
		}
	}
}

猜你喜欢

转载自blog.csdn.net/baiyifeifei/article/details/82556052