[数学-farey序列]Pku3374--Cake Share

http://acm.pku.edu.cn/JudgeOnline/problem?id=3374

 

Cake Share
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2836 Accepted: 700

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≤ ts) 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

Source

 

题目大意:让你找出,分子分母均不大于N的最简真分数中,第k小的是什么。

分析:分子分母均不大于N的最简真分数序列,在数学上称为farey序列,用FN表示。

例如:F3=(0/1,1/3,2/1,2/3,1/1)。

这个序列有以下特征:

1、除了F1,FN序列的分数个数为奇数个,且中间一个必然为1/2,序列左右对称,相对应的两个分数和为1.

2、对于三个连续的分数x1/y1,x2/y2,x3/y3,有x2=x1+x3,y2=y1+y3且x1*y2-x2*y1=1。这保证了序列中的分数都是最简分数。

由性质2,可以得出以下的一个递归构造farey序列的算法:

Procedure make_farey(x1,y1,x2,y2 : integer)

              If x1+x2>N or y1+y2>N then Return
              make_farey(x1,y1,x1+x2,y1+y2)
              inc(total)
              farey [total] = {x1+x2,y1+y2}
make_farey(x1+x2,y1+y2,x2,y2)
       End Procedure.

就这么简单。

其中x1,y1,x2,y2表示第一个分数和第三个分数。

有了这个构造算法,这题就迎刃而解了。

只要构造出序列的前半部分,然后根据性质1,不难得出后半部分。

F5000大约有7600000个数。所以一半只要开4000000就完全足够了。

 

 

codes:

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值