POJ2100 Graveyard Design

  题目大意:给定一个数n,求出一段连续的正整数的平方和等于n的方案数,并输出这些方案,注意输出格式;

    循环判断条件可以适当剪支,提高效率,(1^2+2^2+..n^2)=n*(n+1)*(2n+1)/6;

    尺取时一定要注意循环终止条件的判断。

    

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <ctime>
  6. #include <iostream>
  7. #include <algorithm>
  8. #include <string>
  9. #include <vector>
  10. #include <deque>
  11. #include <list>
  12. #include <set>
  13. #include <map>
  14. #include <stack>
  15. #include <queue>
  16. #include <numeric>
  17. #include <iomanip>
  18. #include <bitset>
  19. #include <sstream>
  20. #include <fstream>
  21. using namespace std;
  22. #define rep(i,a,n) for (int i=a;i<n;i++)
  23. #define per(i,a,n) for (int i=n-1;i>=a;i--)
  24. #define in(n) scanf("%d",&(n))
  25. #define in2(x1,x2) scanf("%d%d",&(x1),&(x2))
  26. #define inll(n) scanf("%I64d",&(n))
  27. #define inll2(x1,x2) scanf("%I64d%I64d",&(x1),&(x2))
  28. #define inlld(n) scanf("%lld",&(n))
  29. #define inlld2(x1,x2) scanf("%lld%lld",&(x1),&(x2))
  30. #define inf(n) scanf("%f",&(n))
  31. #define inf2(x1,x2) scanf("%f%f",&(x1),&(x2))
  32. #define inlf(n) scanf("%lf",&(n))
  33. #define inlf2(x1,x2) scanf("%lf%lf",&(x1),&(x2))
  34. #define inc(str) scanf("%c",&(str))
  35. #define ins(str) scanf("%s",(str))
  36. #define out(x) printf("%d\n",(x))
  37. #define out2(x1,x2) printf("%d %d\n",(x1),(x2))
  38. #define outf(x) printf("%f\n",(x))
  39. #define outlf(x) printf("%lf\n",(x))
  40. #define outlf2(x1,x2) printf("%lf %lf\n",(x1),(x2));
  41. #define outll(x) printf("%I64d\n",(x))
  42. #define outlld(x) printf("%lld\n",(x))
  43. #define outc(str) printf("%c\n",(str))
  44. #define pb push_back
  45. #define mp make_pair
  46. #define fi first
  47. #define se second
  48. #define SZ(x) ((int)(x).size())
  49. #define mem(X,Y) memset(X,Y,sizeof(X));
  50. typedef vector<int> vec;
  51. typedef long long ll;
  52. typedef pair<int,int> P;
  53. const int dx[]={,,-,},dy[]={,,,-};
  54. const int INF=0x3f3f3f3f;
  55. const ll mod=1e9+;
  56. ll powmod(ll a,ll b) {ll res=;a%=mod;for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
  57. const bool AC=true;
  58.  
  59. struct node{
  60. ll num,a,b;
  61. };
  62. node p[];
  63. bool cmp(node x,node y){
  64. return x.num>y.num;
  65. }
  66. int main(){
  67. ll n,s,t,sum,k,cnt;//都设为longlong免得溢出
  68. while(inll(n)!=EOF){
  69. s=t=;sum=;k=;
  70. while(true){
  71. while(sum<n){ //注意循环终止条件
  72. sum+=t*t;
  73. t++;
  74. }
  75. if(sum==n) {
  76. p[k].num=t-s;
  77. p[k].a=s;
  78. p[k++].b=t-;
  79. }
  80. sum-=s*s;
  81. s++;
  82. if(s*s>n) break; //注意循环终止条件
  83. }
  84. cnt=k;
  85. printf("%I64d\n",cnt);//别忘了这个
  86. sort(p,p+k,cmp);
  87. rep(i,,k){
  88. printf("%I64d ",p[i].num);
  89. for(ll j=p[i].a;j<=p[i].b;j++){
  90. printf("%I64d ",j);
  91. }
  92. printf("\n");
  93. }
  94. }
  95. return ;
  96. }

POJ2100 Graveyard Design(尺取法)的更多相关文章

  1. POJ 尺取法

    poj3061 Subsequence 题目链接: http://poj.org/problem?id=3061 挑战P146.题意:给定长度为n的数列整数a0,a1,...,a(n-1)以及整数S, ...

  2. poj 2100 Graveyard Design(尺取法)

    Description King George has recently decided that he would like to have a new design for the royal g ...

  3. poj2100(尺取法)

    题意:选取一系列数,使得这些数的平方和等于n: 解题思路:尺取法扫一遍: #include<iostream> #include<algorithm> using namesp ...

  4. poj 2100(尺取法)

    Graveyard Design Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 6107   Accepted: 1444 ...

  5. POJ_2100_Graveyard_Design_(尺取法)

    描述 http://poj.org/problem?id=2100 求连续平方和=n的序列个数,并输出序列. Graveyard Design Time Limit: 10000MS   Memory ...

  6. 5806 NanoApe Loves Sequence Ⅱ(尺取法)

    传送门 NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K ...

  7. POJ3061 尺取法

    题目大意:从给定序列里找出区间和大于等于S的最小区间的长度. 前阵子在zzuli OJ上见过类似的题,还好当时补题了.尺取法O(n) 的复杂度过掉的.尺取法:从头遍历,如果不满足条件,则将尺子尾 部增 ...

  8. POJ 2739 Sum of Consecutive Prime Numbers(尺取法)

    题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS     Memory Limit: 65536K Description S ...

  9. CF 701C They Are Everywhere(尺取法)

    题目链接: 传送门 They Are Everywhere time limit per test:2 second     memory limit per test:256 megabytes D ...

随机推荐

  1. KACK的处理方法

    demo: .eq { color:#f00;/*标准浏览器*/ color:#f30\0;/*IE8,IE9,opera*/ *color:#c00;/*IE7及IE6*/ _color:#600; ...

  2. debain 解决无法显示中文

    首先先配置编码:vim /etc/locale.gen  去掉前面的# en_US.UTF-8 UTF-8 zh_CN GB2312 zh_CN.GBK GBK zh_CN.UTF-8 UTF-8 然 ...

  3. 移动端app测试

    对于手机项目(应用软件),主要是进行系统测试. 而针对手机应用软件的系统测试,我们通常从如下几个角度开展测试工作: 功能模块测试: 交叉事件测试: 性能测试: 安全测试: 容量测试: 兼容性测试: 接 ...

  4. AD:想两VIA在同一plane层不同连接(两VIA接同网络),一全连接、一花孔接,实现方法

    可以用room方法处理!

  5. Android开发 解决AlertDialog中的EditText无法调出输入法的问题

    在AlertDialog中使用自定义的View,如果View中有EditText,在上面点击,默认是跳不出软键盘的,不是焦点的问题.解决方法,有两种,一是把AlertDialog换成Dialog,但这 ...

  6. rsyslog 一重启就会开始同步之前所有通配的日志文件

    <pre name="code" class="html">[root@dr-mysql01 zjzc_log]# grep '24/Sep/201 ...

  7. 【HDOJ】2526 浪漫手机

    字符串大水题. #include <cstdio> #include <cstring> #include <cstdlib> #define MAXN 105 t ...

  8. 【转】单双精度浮点数的IEEE标准格式

    原文网址:http://blog.chinaunix.net/uid-24118190-id-75212.html 单双精度浮点数的IEEE标准格式 关键字:浮点数 IEEE标准 大多数高级语言按照I ...

  9. win7下自写驱动导致开机蓝屏调试过程

    之前没有接触过驱动调试.这里上手就要解决一个因为某个自定义驱动导致的系统登陆后蓝屏问题,记录下来.   问题: 从客户那边弄来的一个虚拟机,已知是加了我们的驱动之后才会导致蓝屏. 解决过程:   使用 ...

  10. HDU5107---K-short Problem (线段树区间 合并、第k大)

    题意:二维平面上 N 个高度为 Hi 建筑物,M次询问,每次询问输出 位于坐标(x ,y)左下角(也就是xi <= x && yi <= y)的建筑物中的第k高的建筑物的高 ...