纪念邮票

Problem Description

邮局最近推出了一套纪念邮票,这套邮票共有N张,邮票面值各不相同,按编号顺序为1分,2分,...,N分。
小杭是个集邮爱好者,他很喜欢这套邮票,可惜现在他身上只有M分,并不够把全套都买下。他希望尽量买,最好刚好花光所有钱。作为一个集邮爱好者,小杭也不想买的邮票编号断断续续。所以小杭打算买面值a分至b分的b-a+1张连续的邮票,且总价值刚好为M分。
你的任务是求出所有符合要求的方案,以[a,b]的形式输出。

Input

输入有多组测试用例,每组测试用例包含两个数N和M(1<=N,M<=10^9)。

Output

对于每组测试用例输出所有合法方案:[a,b],按a值从小到大输出。

Sample Input

20 15

Sample Output

[1,5]
[4,6]
[7,8]
[15,15]
 1 #include <stdio.h>
 2 #include <math.h>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 struct node {
 7     int a,b;
 8 } ans[1000];
 9 int T,n,m,cnt;
10 
11 bool cmp(const node &x, const node &y) {
12     if ( x.a!=y.a ) return x.a<y.a;
13     return x.b<y.b;
14 }
15 void cal() {
16     cnt=-1;
17     for (int i=0; i<=int( sqrt(2.0)*sqrt(m+0.0) ); ++i) {
18         if ( (2*m)%(i+1)==0  && (2*m/(i+1)-i) >=2 && (2*m/(i+1)-i)%2==0 ) {
19             int start=2*m/(i+1)-i;
20             start/=2;
21             if (i+start<=n) {
22                 ++cnt;
23                 ans[cnt].a=start;
24                 ans[cnt].b=start+i;
25             }
26         }
27     }
28 }
29 
30 int main() {
31     while(scanf("%d%d", &n, &m)!=EOF) {
32         cal();
33         sort(ans, ans+cnt+1,cmp);
34         for (int i=0; i<=cnt; ++i) printf("[%d,%d]\n", ans[i].a, ans[i].b );
35     }
36 
37     return 0;
38 }
View Code

 

posted @ 2013-06-03 17:45  1002liu  阅读(236)  评论(0编辑  收藏  举报