F. Count Prime Pairs

A single point of time: 2.0 sec

Memory Limit: 512 MB

For arrays A , if I J and A I + A J is a prime number, then we said ( I , J ) is a prime number of prime numbers to calculate the array.

Input Format

A first input row n , represents the length of the array, the next n integers, the i number of representatives of A i .
( . 1 n- 100000 , 0 A I 100 )

Output Format

The number of prime numbers output array.

Sample

input
3
1 2 3
output
4

prompt

Sample Description: A . 1 + A 2 , A 2 + A . 1 , A 2 + A . 3 , A . 3 + A 2 are prime numbers, a total of four pairs.

 
#include<iostream>
#include<map>
#include<set>
using namespace std;
const int N=1e5 + 10;
map<int ,int>mp;
set<int>se;
set<int >::iterator it;
int arr[N];
int a[N];
int prime[210]={1,1,0};
int pow(){
    for(int i=0;i*i<=200;i++)
        if(prime[i]==0){
            for(int j=i+i;j<=200;j+=i)
                prime[j]=1;
        }
}
int main(){
    pow();
    int n;
    cin>>n;
    int x;
    for(int i=0;i<n;i++){
        cin>>x;
        mp[x]++;//key value, the number of times that value appears 
        se.insert (X); 
    } 
    int POS = 0 ;
     for (IT = se.begin ();! se.end IT = (); IT ++ ) { 
        A [POS + +] = * IT; 
    } 
    int SUM = 0 ;
     for ( int I = 0 ; I <POS; I ++ ) {
         for ( int J = 0 ; J <POS; J ++ ) {// if i == j when they refers to the same number, i.e. a number appears more than once ,, can push their total number is composed of n * (n-1) * n-otherwise n-; 

IF (Prime [a [I] +! a [J]])
            {
                if(i==j){
                    sum+=mp[a[i]]*(mp[a[i]]-1);
                
                }
                else sum+=mp[a[i]]*mp[a[j]];
            }
        }
    }
    cout<<sum<<endl; 
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Accepting/p/11285536.html