您的位置:首页 > 其它

[POJ 2100] Graveyard Design (Two Pointer)

2016-04-15 22:58 197 查看
POJ - 2100

给出一个数 N,求有多少种连续的平方和加起来为 N

没啥好说的,two pointer搞起

坑点有几个:

1) 这题 POJ上内存给得比较小,所以不能把平方和预处理出来

2) 注意爆 int的问题

3) 多组数据

4) s,t,sum都要从 1开始,否则会 WA

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <queue>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef double DBL;
typedef long double LDBL;
typedef pair<int,int> Pii;
typedef pair<long long,long long> Pll;
#define MST(a,b) memset(a,b,sizeof(a))
#define CLR(a) MST(a,0)
#define Pow2(a) (a*a)

LL N;
vector<Pll> ans;

int main()
{
while(~scanf("%I64d\n", &N))
{
ans.clear();
LL s=1,t=1,sum=1,lim=sqrt((DBL)N)+1;
while(s<lim)
{
while(t<lim&&sum<N)
{
t++;
sum+=t*t;
}
if(sum==N) ans.push_back(make_pair(s,t));
sum-=s*s;
s++;
}
printf("%d\n", ans.size());
for(int i=0; i<ans.size(); i++)
{
printf("%d", ans[i].second-ans[i].first+1);
for(int j=ans[i].first; j<=ans[i].second; j++) printf(" %d", j);
puts("");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: