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 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 s2
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 <= 1014 ).

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。尺取法过程:

  整个过程分为4布:

    1.初始化左右端点

    2.不断扩大右端点,直到满足条件

    3.如果第二步中无法满足条件,则终止,否则更新结果

    4.将左端点扩大1,然后回到第二步

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
#include <math.h>
using namespace std;
typedef long long LL;
LL n;
struct Node{
    LL l,r;
}res[1000007];
int main()
{
    while(scanf("%lld",&n)!=EOF){

        LL l=1,r=1;
        LL len = (int)sqrt(n*1.0)+1;
        LL sum = 0;
        int cnt=0;
        while(l<=len){
            while(r<=len&&sum<n){
                sum+=r*r;
                r++;
            }
            if(sum<n) break;
            if(sum==n){
                cnt++;
                res[cnt].l = l;
                res[cnt].r = r;
            }
            sum-=l*l;
            l++;
        }
        printf("%d\n",cnt);
        for(int i=1;i<=cnt;i++){
            printf("%d ",res[i].r-res[i].l);
            for(int j=res[i].l;j<res[i].r-1;j++){
                printf("%d ",j);
            }
            printf("%d\n",res[i].r-1);
        }
    }
    return 0;
}
时间: 2024-10-05 19:11:27

poj 2100(尺取法)的相关文章

poj 2100 尺取法(尺度法)

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

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]

POJ 3320 尺取法,Hash,map标记

1.POJ 3320 2.链接:http://poj.org/problem?id=3320 3.总结:尺取法,Hash,map标记 看书复习,p页书,一页有一个知识点,连续看求最少多少页看完所有知识点 必须说,STL够屌.. #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #include<cstdio>

POJ 2566(尺取法

自己看了半天并没有看出这题怎么用尺取法(虽然一看就觉得肯定是尺取法..),由于是绝对值,那么在计算的时候头和尾的实际位置并不重要,而应用尺取法这个数列肯定得是单调,那么我们把前缀和处理出来排序就可以直接应用尺取法了 #include<iostream> #include<cstdio> #include<algorithm> #include<queue> #include<utility> #include<vector> #inc

poj 3320 尺取法

容易联想到尺取法,因为假设从第s页开始阅读至少需要读到t页才能覆盖所有知识点的话,那么如果从s+1页开始阅读,至少要读到t'>=t的位置.于是可以考虑用map维护一下. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <map> 5 using namespace std; 6 7 const int INF = 1111111; 8 const int

poj 3061 尺取法或二分

经典尺取法,复杂度O(n). 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 const int INF = 999999; 7 const int N = 100000; 8 int a[N]; 9 10 int main () 11 { 12 int t; 13 scanf("%d", &t); 14 whi

POJ 3320 (尺取法+Hash)

题目链接: http://poj.org/problem?id=3320 题目大意:一本书有P页,每页有个知识点,知识点可以重复.问至少连续读几页,使得覆盖全部知识点. 解题思路: 知识点是有重复的,因此需要统计不重复元素个数,而且需要记录重复个数. 最好能及时O(1)反馈不重复的个数.那么毫无疑问,得使用Hash. 推荐使用map,既能Hash,也能记录对于每个key的个数. 尺取的思路: ①不停扩展R,并把扫过知识点丢到map里,直到map的size符合要求. ②更新结果. ②L++,map

POJ 2566 尺取法(进阶题)

Bound Found Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4297   Accepted: 1351   Special Judge Description Signals of most probably extra-terrestrial origin have been received and digitalized by The Aeronautic and Space Administration

POJ 3276 Face The Right Way (常用技巧-尺取法)

[题目链接]:click here~~ [题目大意]:N头牛排成一列1<=N<=5000.每头牛或者向前或者向后.为了让所有牛都 面向前方,农夫每次可以将K头连续的牛转向1<=K<=N,求操作的最少 次数M和对应的最小K. [思路]:由于交换区间翻转顺序对结果没影响,所以从左往右对于需要  翻转的牛进行反转,同时记录对该区间其他牛的影响即cal中的sum, 对于最后部分无法翻转的区间检查是否有反向牛,若有则方案失败.此题思想值得细细思考,常常有一种无限状态,化为有限状态. 代码: