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

来源:互联网 发布:安卓c语言编程软件 编辑:程序博客网 时间:2024/05/16 14:27

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

 

Cake Share
Time Limit: 1000MS Memory Limit: 65536KTotal 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 171112

Sample Output

0/13/51/1No Solution

Source

POJ Monthly--2007.09.09, Updog

 

题目大意:让你找出,分子分母均不大于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:

 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 苹果手机自带相机拍照会晃屏怎么办 苹果x手机壳拆不下来怎么办 苹果手机5c屏幕没有反应怎么办 玩穿越火线屏幕两边是黑的怎么办? 8g内存只有2g可用怎么办? 三星7e微信分身打不开怎么办? 光猫的网口1不亮怎么办 两年前的发票发现名头有错误怎么办 苹果5s手机通话声音小怎么办 华为全网通手机电信卡打不了怎么办 合约机移动违约不返话费我该怎么办 电信手机卡合约套餐要到期了怎么办 苹果6s联通4g网速慢怎么办 营业厅买到的不是全网通手机怎么办 全网通手机联通卡被禁用怎么办 红米5手机关机充电自动开机怎么办 华为平板怎么解锁密码忘了怎么办 华为荣耀手机开锁密码忘记了怎么办 畅玩7x密码忘了怎么办 过了时的手机没有刷机包怎么办? 刷了个刷机包游戏玩不了了怎么办? 华为麦芒5手机外放声音小怎么办 微信显示存储卡已拔出怎么办 储存卡已拔出微信头像不可用怎么办 智能手机的电话卡取不出来了怎么办 换了苹果手机通讯录没了怎么办 手机玻璃膜一角翘起来了怎么办 华为畅玩7x耗电快怎么办 魅蓝5s充电器死机了怎么办 苹果手机乐动力不计步数怎么办 意大利居留按手印时间过了怎么办 酷派t1手机解析包出现问题怎么办 p新买的手机壳有味怎么办 门锁钥匙口竖着钥匙放不进去怎么办 摩拜单车被别人骑走了怎么办 捡到苹果8p手机怎么办才能自己用 用力按压导致玻尿酸变形移位怎么办 华为麦芒5应用锁密码忘了怎么办 华为麦芒6应用锁密码忘了怎么办 华为手机的设置不在桌面了怎么办 华为手机所有应用都不在桌面怎么办