Cod sursa(job #2847401)

Utilizator SabailaCalinSabaila Calin SabailaCalin Data 10 februarie 2022 19:28:36
Problema Pairs Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.24 kb
#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

ifstream f ("pairs.in");
ofstream g ("pairs.out");

const int N_MAX = 100001;

int n;
bool isPrime[N_MAX];
vector <int> numbers;

void Eratosthenes()
{
    for (int i = 2; i <= N_MAX / 2; i++)
    {
        if (isPrime[i] == 0)
        {
            for (int j = 2 * i; j <= N_MAX; j += i)
            {
                isPrime[j] = 1;
            }
        }
    }
}

bool PrimeBetween(int x, int y)
{
    if (x == y)
    {
        return false;
    }

    if (isPrime[x] == 1 && isPrime[y] == 1)
    {
        return false;
    }

    if (isPrime[x] == 0 || isPrime[y] == 0)
    {
        if (x % y == 0)
        {
            return false;
        }
        if (y % x == 0)
        {
            return false;
        }
    }
    return true;
}

int main()
{
    Eratosthenes();
    f >> n;
    int cnt = 0;
    for (int i = 1; i <= n; i++)
    {
        int x;
        f >> x;
        for (unsigned int j = 0; j < numbers.size(); j++)
        {
            if (PrimeBetween(x, numbers[j]) == true)
            {
                cnt++;
            }
        }
        numbers.push_back(x);
    }
    g << cnt;
}