Language:
Cake Share
Description Updog prepared to enjoy his delicious supper. At the very time he was ready to eat, a serious accident occurred—GtDzx appeared!! GtDzx declared his hadn't eaten anything for 3 days (obviously he was lying) and required Updog to share the cake with him. Further more, he threatened Updog that if Updog refused him, he would delete Updog's account in POJ! Thus Updog had no choice. Updog intended to cut the cake into s (s ≥ 1) pieces evenly, and then gave t(0≤ t ≤ s) pieces to GtDzx. Apparently GtDzx might get different amount of cake for different s and t. Note that s = 12, t = 4 and s = 6, t = 2 will be regarded as the same case since GtDzx will get equal amount in the two cases. Updog wouldn't separate the cake into more than N pieces. After sorted all available cases according to the amount of cake for GtDzx, in the first case no cake to gave to GtDzx (t = 0) and in the last case GtDzx would get the whole cake (s = t). Updog wondered that how much cake GtDzx would get in the k-th case. Input The first line of the input file contains two integers N (1 ≤ N ≤ 5000) and C(0 ≤ C≤ 3000). The following C lines each contains a positive integer describe C query respectively. The i-th query ki is to ask GtDzx's share of whole cake in the ki-th case . Output Answer each query in a separated line, according to the order in the input. Sample Input 5 4 1 7 11 12 Sample Output 0/1 3/5 1/1 No Solution |
对于n 求这个Farey序列中第k大的值
特判1 然后将某组序列分为两部分去求
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 10010
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
using namespace std;
int Read()
{
char c = getchar();
while (c < '0' || c > '9') c = getchar();
int x = 0;
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x;
}
void Print(int a)
{
if(a>9)
Print(a/10);
putchar(a%10+'0');
}
int total,n,farey[4000000][2],all;
void makefarey(int x1,int y1,int x2,int y2)
{
if(x1+x2>n||y1+y2>n) return;
makefarey(x1,y1,x1+x2,y1+y2);
total++;
farey[total][0]=x1+x2;
farey[total][1]=y1+y2;
makefarey(x1+x2,y1+y2,x2,y2);
}
int main()
{
//fread;
int q;
while(scanf("%d%d",&n,&q)!=EOF)
{
if(n==1)
{
while(q--)
{
int k;
scanf("%d",&k);
if(k==1) printf("0/1\n");
else if(k==2) printf("1/1\n");
else puts("No Solution");
}
continue;
}
total=1;
farey[1][0]=0;
farey[1][1]=1;
makefarey(0,1,1,2);
farey[total+1][0]=1;
farey[total+1][1]=2;
all=total*2+1;
while(q--)
{
int k;
scanf("%d",&k);
if(k>all) puts("No Solution");
else if(k<=total+1) printf("%d/%d\n",farey[k][0],farey[k][1]);
else printf("%d/%d\n",farey[all-k+1][1]-farey[all-k+1][0],farey[all-k+1][1]);
}
}
return 0;
}