1. 程式人生 > >計蒜客 31452 - Supreme Number - [簡單數學][2018ICPC沈陽網絡預賽K題]

計蒜客 31452 - Supreme Number - [簡單數學][2018ICPC沈陽網絡預賽K題]

not sin 數學 output 鏈接 pri bst char order

題目鏈接: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\n",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\n",kase,n[upper_bound(n+1,n+21,num)-(n+1)]);
        }
    }
}

計蒜客 31452 - Supreme Number - [簡單數學][2018ICPC沈陽網絡預賽K題]