Pagini recente » Cod sursa (job #2531129) | Cod sursa (job #528329) | Cod sursa (job #1261296) | Cod sursa (job #1609251) | Cod sursa (job #2847401)
#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;
}