Cod sursa(job #3259462)

Utilizator AnaGrigorieAna Teodora Grigorie AnaGrigorie Data 26 noiembrie 2024 14:07:48
Problema Frac Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <fstream>
#include <cmath>
using namespace std;
ifstream fin("frac.in");
ofstream fout("frac.out");
long long n,p,sol,ok,a,b,r;
bool x[1000000];
int main()
{
    fin>>n>>p;
    sol=1;
    p--;
    long long i=2;
    while(p>0)
    {
        ok=1;
        for(int j=2;j<=i-1;j++)
        {
            if(i%j==0 && x[j]==1)
            {
                ok=0;
                break;
            }
        }
        if(ok==1)
        {
            a=n;
            b=i;
            while(b!=0)
            {
                r=a%b;
                a=b;
                b=r;
            }
            if(a==1)
            {
                p--;
                sol=i;
            }
            else
            {
                x[i]=1;
            }

        }
        i++;
    }
    fout<<sol;
    return 0;
}