Cod sursa(job #3208726)

Utilizator XSZeroBurghelea Radu-Theodor XSZero Data 29 februarie 2024 16:19:31
Problema Pairs Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.22 kb
#include <bits/stdc++.h>
using namespace std;

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

vector<vector<int> > ciur(1000000);
int a[100001],n;
long long nr;
bool isprim(int a1,int b)
{
    int l=0,j=0;
    if(ciur[a1].empty())
    {
        if(b%a1!=0) return 1;
        return 0;
    }
    if(ciur[b].empty())
    {
        if(a1%b!=0) return 1;
        return 0;
    }
    while(l<ciur[a1].size() && j<ciur[b].size())
    {
        if(ciur[a1][l]==ciur[b][j]) return 0;
        if(ciur[a1][l]<ciur[b][j]) l++;
        else if(ciur[a1][l]>ciur[b][j]) j++;
    }
    return 1;
}
int main()
{
    f>>n;
    int maxi=-1;
    for(int i=1;i<=n;i++)
        {
            f>>a[i];
            maxi=max(maxi,a[i]);
        }
    ciur.resize(maxi+1);
    for(int div=2;div<=maxi/2;div++)
        if(ciur[div].empty())
        {
            for(int i=div+div;i<=maxi;i+=div)
                ciur[i].push_back(div);
        }
    for(int i=1;i<n;i++)
    {
        //if(ciur[a[i]].empty()) nr+=n-i;
        for(int j=i+1;j<=n;j++)
            if(isprim(a[i],a[j]))
            {
                cout<<a[i]<<" "<<a[j]<<'\n';
                nr++;
            }
    }
    cout<<nr;
    return 0;
}