计蒜客 Supreme Number (2018-ICPC-沈阳-网络赛)

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 沈阳赛区网络预赛


题意:输入n,找出最大的小于等于n的满足条件的值。条件是:这个数字里所有子序列都必须是素数

那么既然是素数,并且是子序列而不是子串,那么必有蹊跷

既然要是素数,那么除了1以外的数都不能出现两次,并且出现的数字只能是  1 2 3 5 7

两位数的时候,十位是1 的话,个位可以是 1 3 7,十位是 2 的话个位只能是 3,并且我们知道 2 只能出现在最高位,否则子序列里就会有偶数。5也只能做最高位,否则就会出现 5 的倍数。所以我们可以断定符合要求的数一定很少,且不可能超过 6 位,那么暴力打个表出来就很明显了,符合的只有这么些

 {1,2,3,5,7,11,13,17,23,31,37,53,71,73,113,131,137,173,311,317}

#include
using namespace std;

int num[] = {1,2,3,5,7,11,13,17,23,31,37,53,71,73,113,131,137,173,311,317};

char str[200];

int main() {
	int T,Case = 1;
	scanf("%d",&T);
	while(T--) {
		scanf("%s",str);
		int len = strlen(str);
		printf("Case #%d: ",Case++);
		if(len >= 4) {
			puts("317");
		} else {
			int x = 0;
			for(int i = 0; i < len; i++) {
				x = x * 10 + (str[i] - '0');
			}
			if(x > 317) {
				puts("317");
			} else {
				for(int i = 0; i < 20; i++) {
					if(num[i] == x) {
						printf("%d\n",num[i]);
						break;
					}
					if(num[i] > x) {
						printf("%d\n",num[i - 1]);
						break;
					}
				}
			}
		}
	}
	return 0;
}


 

你可能感兴趣的:(数学,思维题)