poj 2100(尺取法)
2024-08-31 04:01:35
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.
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.
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[];
int main()
{
while(scanf("%lld",&n)!=EOF){ LL l=,r=;
LL len = (int)sqrt(n*1.0)+;
LL sum = ;
int cnt=;
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=;i<=cnt;i++){
printf("%d ",res[i].r-res[i].l);
for(int j=res[i].l;j<res[i].r-;j++){
printf("%d ",j);
}
printf("%d\n",res[i].r-);
}
}
return ;
}
最新文章
- Date.parse
- python——连接MySQL数据库
- vue之自定义指令directive
- c# List AddRange
- html知识2
- Nginx编译参数大全 configure参数中文详解
- Asp.net MVC中Route的理解
- 【CF493E】【数学】Vasya and Polynomial
- 【转】NDK编译可执行文件在Android L中运行显示error: only position independent executables (PIE) are supported.失败问题解决办法。
- 关于url拼接传参数和利用view的字典传参数时,模板获取数据的方式问题
- Android设备管理器漏洞2--禁止用户取消激活设备管理器
- 20165306 Exp4 恶意代码分析
- L1-060 心理阴影面积
- 06_Hadoop分布式文件系统HDFS架构讲解
- HomeBrew 安转beta版软件
- Codeforce 294A - Shaass and Oskols (模拟)
- mysql 5.17 的update失败问题
- git初级浅入其常用操作
- 网络编程(socket,套接字)
- 个人项目-词频统计(语言:C++)