poj 3374 Cake Share(Farey序列第k大)

Language:
Cake Share
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3157 Accepted: 833

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 ≥ 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 = 12, = 4 and = 6, = 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 (= 0) and in the last case GtDzx would get the whole cake (= 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 (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;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值