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

题解:打表

暴力找出规律:可以看出这样的数只能由1,2,3,5,71,2,3,5,7组成,一位数符合的有:1 2 3 5 7;两位数符合的有:11 13 17 23 31 37 53 71 73;三位数符合的有:113 131 137 173 311 317;四位数没有符合的。任意的四位数都不符合,那么只要超过四位的数字,定能找到一个四位数的子串,即定有不是素数的子串,所以只要超过四位数,答案就是317.。

官方题解:由于任意两个不为11的数字构成的两位数一定可以被1111整除,所以答案在除11以外的数字只能出现一次;11最多出现两次,因为111111可以被33整除;而2,5,72,5,7三者一点不会有两者同时出现。因此满足条件的整数不会超过四位。

#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;
	int n;
	cin>>t;
	while(t--)
	{
		cin>>n;
		if(n>999)
		{
			cout<<"Case #"<<cas++<<": "<<317<<endl;
		}
		else
		{
			for(int i=0;i<20;i++)
			{
				if(n>=num[i])
				{
					cout<<"Case #"<<cas++<<": "<<num[i]<<endl;
					break;
				}
					
			}
		}
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值