Graveyard Design POJ - 2100

King George has recently decided that he would like to have a new design for the royal graveyard. The graveyard must consist of several sections, each of which must be a square of graves. All sections must have different number of graves.
After a consultation with his astrologer, King George decided that the lengths of section sides must be a sequence of successive positive integer numbers. A section with side length s contains s 2 graves. George has estimated the total number of graves that will be located on the graveyard and now wants to know all possible graveyard designs satisfying the condition. You were asked to find them.Input

Input file contains n --- the number of graves to be located in the graveyard (1 <= n <= 10 14 ).

Output

On the first line of the output file print k --- the number of possible graveyard designs. Next k lines must contain the descriptions of the graveyards. Each line must start with l --- the number of sections in the corresponding graveyard, followed by l integers --- the lengths of section sides (successive positive integer numbers). Output line‘s in descending order of l.

Sample Input

2030

Sample Output

2
4 21 22 23 24
3 25 26 27
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<string>
 5 #include<cstring>
 6 #include<cmath>
 7 #define MAX 10000005
 8 using namespace std;
 9 typedef long long ll;
10
11 ll n;
12 ll a[MAX],b[MAX],c[MAX];
13
14 int main()
15 {  while(~scanf("%lld",&n)){
16        int temp=sqrt(n)+1;
17        ll l=1,r=1,t=0;    //用long long!!!调了一个多小时。。。
18        ll ans=0;
19        while(r<=temp){
20               if(r*r>n) break;
21               ans+=r*r;
22               while(ans>n){
23                    ans-=l*l;
24                    l++;
25               }
26               r++;
27               if(ans==n){
28                    t++;
29                    b[t]=r;
30                    c[t]=r-l;
31               }
32        }
33        if(t==0) printf("0\n");
34        else{
35            printf("%d\n",t);
36            for(int i=1;i<=t;i++){
37                  printf("%lld",c[i]);
38                  for(int j=c[i];j>0;j--) printf(" %lld",b[i]-j);
39                  printf("\n");
40              }
41        }
42    }
43 }
时间: 2024-10-19 20:15:23

Graveyard Design POJ - 2100的相关文章

Greedy:Graveyard Design(POJ 2100)

墓地 题目大意,给定一个整数,要你找出他的平方和组合 太简单了....不过一开始我储存平方和想降低时间,后来发现会超内存,直接用时间换空间了,游标卡尺法 1 #include <iostream> 2 #include <functional> 3 #include <algorithm> 4 #define MAX_N 10000001 5 6 using namespace std; 7 typedef long long LL_INT; 8 9 static in

poj 2100 Graveyard Design

Graveyard Design Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 7357   Accepted: 1816 Case Time Limit: 2000MS Description King George has recently decided that he would like to have a new design for the royal graveyard. The graveyard m

POJ2100 Graveyard Design(尺取法)

POJ2100 Graveyard Design 题目大意:给定一个数n,求出一段连续的正整数的平方和等于n的方案数,并输出这些方案,注意输出格式: 循环判断条件可以适当剪支,提高效率,(1^2+2^2+..n^2)=n*(n+1)*(2n+1)/6; 尺取时一定要注意循环终止条件的判断. #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <

poj 2100(尺取法)

Graveyard Design Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 6107   Accepted: 1444 Case Time Limit: 2000MS Description King George has recently decided that he would like to have a new design for the royal graveyard. The graveyard m

poj 2100 尺取法(尺度法)

poj 2100 尺取法(尺度法) 题意 给你一个数N,然后找到一个连续的序列,使得这个序列中的数的平方和等于N. 输出需要注意的是第一行为解的个数,剩下的每行先输出序列的长度,然后输出序列.按照序列的长度进行降序输出. 解题思路 没啥好说的,使用尺度法,进行枚举各个区间上的数. 需要注意的是数字1的答案为: 1 1 1 代码实现 #include<cmath> #include<cstdio> #include<cstring> #include<algorit

poj 2100 Graveyard Design(尺取法)

Description King George has recently decided that he would like to have a new design for the royal graveyard. The graveyard must consist of several sections, each of which must be a square of graves. All sections must have different number of graves.

poj 2100 尺取法 一个数字拆成连续数字平方和

题意:将一个数拆成若干个连续数字的平方和. 用尺取法枚举区间,复杂度为O(n),时限10s,3s多ac. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <cmath> 5 using namespace std; 6 7 const int N = 100; 8 9 struct Node 10 { 11 int from, to; 12 } node[N]

POJ2100——尺取

Graveyard Design 题目链接:http://poj.org/problem?id=2100 题目大意:给定一个数,求出所有连续区间的平方和等于该数. 解题思路:尺取法,当区间小于给定数,区间r++,区间和+=r*r:当区间大于给定数,区间和-=l*l,区间l++. 代码如下: #include<stdio.h> #include<math.h> #include<algorithm> using namespace std; struct node { l

《挑战程序设计竞赛》课后练习题解集——3.2 常用技巧精选(一)

常用技巧精选(一) 尺取法 POJ 2566  给出一个长度n(<=1e5)的数列,每个数的大小在-1e4-1e4之间,给出若干询问值,要求一段字串和,它的绝对值与询问值最接近 好题目.由于数列有正有负,所以不能直接二分或尺取.考虑对前缀和排序 得到一个新数列,此时新数列任意一段子串都对应原数列的一个子串,当左右端点的下标颠倒时,字串和也会添一个负号,但是最后要取绝对值所以可以忽略.最后对新数列尺取即可 1 #include <algorithm> 2 #include <cstd