http://acm.pku.edu.cn/JudgeOnline/problem?id=3374
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≤ 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
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)
就这么简单。
其中x1,y1,x2,y2表示第一个分数和第三个分数。
有了这个构造算法,这题就迎刃而解了。
只要构造出序列的前半部分,然后根据性质1,不难得出后半部分。
F5000大约有7600000个数。所以一半只要开4000000就完全足够了。
codes: