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 N as the supreme number if and only if each number made up of an non-empty subsequence of all the numeric digits of N must be either a prime number or 11.

For example, 17 is a supreme number because 1, 7, 17 are all prime numbers or 1, and 19 is not, because 9 is not a prime number.

Now you are given an integer  (2≤N≤10100), could you find the maximal supreme number that does not exceed N?

Input

In the first line, there is an integer T (T≤100000) indicating the numbers of test cases.

In the following TT lines, there is an integer N (2≤N≤10^100).

Output

For each test case print "Case #x: y", in which x is the order number of the test case and y is the answer.

样例输入

2
6
100

样例输出

Case #1: 5
Case #2: 73

题目来源

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

题意

定义supreme number:一个数是素数,这个数的每一项都是1或素数,并且由这个数的每一位数组成的序列中,取出组成的任意子序列组成一个数,都是素数

求小于等于N的最大的supreme number

思路

打表找规律吧。。。最后会发现只有20个数符合supreme number的定义,然后把这20的数存入数组,输入整数N,在数组中查找符合要求的数就可以了

AC代码

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define ll long long
#define ull unsigned long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x7f7f7f7f
#define lson o<<1
#define rson o<<1|1
const double E=exp(1);
const int maxn=1e6+10;
const int mod=1e9+7;
using namespace std;
int vis[30]={1,2,3,5,7,11,13,17,23,31,37,53,71,73,113,131,137,173,311,317};
char num[maxn];
int main(int argc, char const *argv[])
{
	ios::sync_with_stdio(false);
	int t;
	cin>>t;
	int _=0;
	while(t--)
	{
		int ans=0;
		cin>>num;
		int l=strlen(num);
		cout<<"Case #"<<++_<<": ";
		if(l>3)
			cout<<317<<endl;
		else
		{
			ans=0;
			int res=10;
			for(int i=0;i<l;i++)
				ans=ans*res+num[i]-'0';
			if(ans>=317)
				cout<<317<<endl;
			else
			{
				for(int i=0;i<21;i++)
					if(vis[i]<=ans&&vis[i+1]>ans)
						cout<<vis[i]<<endl;
			}
		}
	}
	return 0;
}

 

转载于:https://www.cnblogs.com/Friends-A/p/10324360.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值