Cod sursa(job #235547)

Utilizator petrepetreoctav d petrepetre Data 24 decembrie 2008 13:47:53
Problema Frac Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.72 kb
#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;

int coprime(long long a, long long b);
int main () {
    ifstream I("frac.in");
    ofstream O("frac.out");    
    long long n, p ,ct=0,i=1,ct2;
    I>>n>>p;
    
    while (i<=p) {
          ct++;
          if (coprime(ct,n)==1)
          i++;
    }
    O<<ct;
}

int coprime(long long a,long long b) {
    long long max,i;
    double d;
    	if(b>a){
            max=a;
            a=b;
            b=max;
    }
    d=sqrt(a);
    max=0;
    
    for (i=2;i<=d;i++) {
        if(a%i==0 && b%i==0)
        max=1;
    }
    if(max==1)
    return 0;
    else
    return 1;
}