计蒜客 31452 - Supreme Number - [简单数学][2018ICPC沈阳网络预赛K题]

题目链接:https://nanti.jisuanke.com/t/31452

A prime number (or a prime) is a natural number greater than $1$ 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 $1$.

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 N (2≤N≤1e100), could you find the maximal supreme number that does not exceed $N$?

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

In the following T lines, there is an integer N (2≤N≤10100).

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.

注意:

子序列(Subsequence)子串(Substring)是不一样的,子序列可以是不连续的。

题意:

若一个数的所有非空子序列是素数(或者 $1$),则称它为“supreme number”,

现在给出一个 $N$,要求不大于 $N$ 的最大的supreme number。

题解:

考虑五位数:首先由于2,5,7只要有两个同时出现,就不行,所以2,5,7只能挑一个;又3不能出现超过1次,所以只能要一个3;那么剩下3个空位只能填1,但一旦有111就不是素数,就不行。

所以超过四位的数统统不行,则只要暴力把四位以内的数全部找出来即可。

AC代码:

#include<bits/stdc++.h>
using namespace std;
int n[]={,,,,,,,,,,,,,,,,,,,,};
char str[];
int main()
{
int T;
cin>>T;
for(int kase=;kase<=T;kase++)
{
scanf("%s",str);
int len=strlen(str);
if(len>=) printf("Case #%d: %d\n",kase,n[]);
else
{
int num=,k=;
for(int i=len-;i>=;i--)
{
num+=(str[i]-'')*k;
k*=;
}
printf("Case #%d: %d\n",kase,n[upper_bound(n+,n+,num)-n-]);
}
}
}
上一篇:微信小程序 自定义导航组件 nav头部 全面屏设计


下一篇:Ubuntu14下LAMP环境的安装以及yaf扩展的安装