Huawei OJ: Number of prime pairs

Source: Internet
Author: User

Number of prime pairs

Title Description
If the number of two positive integers is a prime number, then these two positive integers are called "prime mates", such as 2 and 5, 6, and 13, which can be used for communication encryption. Now the password learn to ask you to design a program, from the existing n (n is even) a number of positive integers selected to form a "prime companion", the selection of a variety of options, for example, there are 4 positive integers: 2,5,6,13, if the 5 and 6 in a group can only get a group of "prime Companion", and 2 and 5, The 6 and 13 groups will get two sets of "prime Companion", the program that can form "prime companion" is called "Best plan", of course, the Password society wants you to find out "best plan".

Input:

There is a positive even N (n≤100) that represents the number of natural numbers to select. The following gives a specific number, ranging from [2,30000].

Output:

Output an integer k that represents the logarithm of the "best plan" that you are seeking to make up the "prime companion".

Input Description:

Enter a description
1 Enter a positive even n
2 Enter n integers



Output Description:

The "best scheme" is the logarithm of the "prime Companion".


Input Example:
42 5 6 13

Output Example:
2
Solving
Not understanding

ImportJava.util.*; Public classmain{ Public Static voidMain (string[] args) {Scanner sc=NewScanner (system.in);  while(Sc.hasnext ()) {intLength =Sc.nextint (); int[] In_num =New int[length];  for(intI =0;i<length;i++) {In_num[i]=Sc.nextint (); }            intMax_count =Getnumofprime (In_num);        System.out.println (Max_count);    } sc.close (); }         Private Static  BooleanIsPrime (intnum) {         for(inti = 2;i<=math.sqrt (num); i++){            if(num%i = = 0){                return false; }        }        return true; }         Private Static intGetnumofprime (int[] in_num) {        intLength =in_num.length; int[] DP =New int[Length+1];  for(inti = 0;i<dp.length;i++) {Dp[i]= 0; }        intCount = 0;  for(intj = length-2;j>=0;j--){             for(intK = length-1;k>j;k--){                if(IsPrime (in_num[j]+In_num[k])) {Count= Dp[j+1]-dp[k-1]+dp[k+1]+1; }                Else{Count= Dp[j+1]; }                                 if(count>Dp[j]) {Dp[j]=count; }            }                               }        returnDp[0]; }     }

Huawei OJ: Number of prime pairs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.