[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
给出一个数 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; }
相关文章推荐
- JAVA第五次作业
- mysql5.5数据库data目录迁移方法详解
- nyoj 925 国王的烦恼
- CardView间距问题
- iOS tableViewCell自适应高度 第三发类库
- introduction to anaconda
- JAVA用双栈实现队列,纪念我挂掉的第一次腾讯面试题
- hdu 1176 免费馅饼(DP)
- hdu 1574(01背包)
- SPARK如何使用AKKA实现进程、节点通信
- Node.js学习 - Multiprocess
- avrdude下载错误及解决
- Android UI基础 --基本控件
- 48. Rotate Image
- 数据结构——用数组实现的栈的基础功能
- 第三百七十八天 how can I 坚持
- 我的hadoop大数据之路(三)
- OC中nil,Nil,NULL的区别
- android ListView 几个重要属性
- Centos7下的systemctl命令与service和chkconfig