-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathsecventa farey.cpp
71 lines (62 loc) · 1.39 KB
/
secventa farey.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# include <bits/stdc++.h>
# define NR 40005
using namespace std;
ifstream f("farey.in");
ofstream g("farey.out");
struct elem {
int a, b;
double rap;
}aux[NR];
bool cmp (elem x, elem y) {
return x.rap < y.rap;
}
int i,j,n,m,a,VV,X,nrAux,nr,IND,ci,cs,mij,K;
int dp[NR];
int numara (int X) {
int sol=0;
double A, Aintreg;
memset (dp, 0, sizeof(dp));
for (int i=1; i<=n; ++i) {
A=(double)X * i / n;
Aintreg=floor(A);
if (A==Aintreg) --Aintreg;
dp[i]+=Aintreg;
sol+=dp[i];
for (int j=2; i*j<=n; ++j)
dp[i*j]-=dp[i];
}
return sol;
}
int cmmdc (int a, int b) {
int R;
while (b) {
R=a%b;
a=b;
b=R;
}
return a;
}
int main ()
{
f>>n>>K;
ci=1; cs=n; //caut X
while (ci<=cs) {
mij=(ci+cs)/2;
nr=numara(mij);
if (nr < K) ci=mij+1, X=mij, nrAux=nr;
else cs=mij-1;
}
//vrem sa vedem daca fractiile cu numitorul i
//pot avea un numarator si sa fie cuprinse intre X/n si (X+1)/n
for (i=1; i<=n; ++i) {
a=ceil((double)X*i/n);
if (cmmdc (a, i)!=1) continue;
if ((double)a/i > (double)(X+1)/n) continue;
++VV;
aux[VV].a=a; aux[VV].b=i; aux[VV].rap=(double)a/i;
}
IND=K-nrAux;
nth_element (aux+1, aux+IND, aux+VV+1, cmp);
g<<aux[IND].a<<" "<<aux[IND].b<<"\n";
return 0;
}