计蒜客 31452

题目链接: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[21]={0,1,2,3,5,7,11,13,17,23,31,37,53,71,73,113,131,137,173,311,317};
char str[105];
int main()
{
    int T;
    cin>>T;
    for(int kase=1;kase<=T;kase++)
    {
        scanf("%s",str);
        int len=strlen(str);
        if(len>=4) printf("Case #%d: %d
",kase,n[20]);
        else
        {
            int num=0,k=1;
            for(int i=len-1;i>=0;i--)
            {
                num+=(str[i]-'0')*k;
                k*=10;
            }
            printf("Case #%d: %d
",kase,n[upper_bound(n+1,n+21,num)-n-1]);
        }
    }
}
原文地址:https://www.cnblogs.com/dilthey/p/9608955.html