POJ_2100_Graveyard_Design_(尺取法)
描述
http://poj.org/problem?id=2100
求连续平方和=n的序列个数,并输出序列.
| Time Limit: 10000MS | Memory Limit: 64000K | |
| Total Submissions: 5987 | Accepted: 1416 | |
| Case Time Limit: 2000MS | ||
Description
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
Output
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
Source
分析
直接尺取.
注意:
1.如果要用开根计算的话要写成 " ll ub=(ll)sqrt(n*1.0); "写成 " ll ub=sqrt(n); "会CE.
所以干脆写成 " r*r<=n "
#include<cstdio>
#include<queue>
#define ll long long
using std :: queue; struct node
{
ll len,fst;
node() {}
node(ll a,ll b) : len(a),fst(b) {}
};
queue <node> q;
ll n; inline ll val(ll x) { return x*x; } int main()
{
#ifndef ONLINE_JUDGE
freopen("grave.in","r",stdin);
freopen("grave.out","w",stdout);
#endif
scanf("%lld",&n);
ll l=,r=,k=,len=;
ll sum=;
while(val(r)<=n)
{
if(sum<n)
{
sum+=val(++r);
len++;
}
else if(sum>n)
{
sum-=val(l++);
len--;
}
else
{
q.push(node(len,l));
k++;
sum-=val(l++);
len--;
}
}
printf("%lld",k);
while(!q.empty())
{
node t=q.front(); q.pop();
ll len=t.len,fst=t.fst;
printf("\n%lld ",len);
for(ll i=;i<len;i++) printf("%lld ",fst+i);
}
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return ;
}
POJ_2100_Graveyard_Design_(尺取法)的更多相关文章
- 5806 NanoApe Loves Sequence Ⅱ(尺取法)
传送门 NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/131072 K ...
- POJ3061 尺取法
题目大意:从给定序列里找出区间和大于等于S的最小区间的长度. 前阵子在zzuli OJ上见过类似的题,还好当时补题了.尺取法O(n) 的复杂度过掉的.尺取法:从头遍历,如果不满足条件,则将尺子尾 部增 ...
- POJ 2739 Sum of Consecutive Prime Numbers(尺取法)
题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Description S ...
- CF 701C They Are Everywhere(尺取法)
题目链接: 传送门 They Are Everywhere time limit per test:2 second memory limit per test:256 megabytes D ...
- nyoj133_子序列_离散化_尺取法
子序列 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述 给定一个序列,请你求出该序列的一个连续的子序列,使原串中出现的所有元素皆在该子序列中出现过至少1次. 如2 8 ...
- Codeforces 676C Vasya and String(尺取法)
题目大概说给一个由a和b组成的字符串,最多能改变其中的k个字符,问通过改变能得到的最长连续且相同的字符串是多长. 用尺取法,改变成a和改变成b分别做一次:双指针i和j,j不停++,然后如果遇到需要改变 ...
- POJ 3061 (二分+前缀和or尺取法)
题目链接: http://poj.org/problem?id=3061 题目大意:找到最短的序列长度,使得序列元素和大于S. 解题思路: 两种思路. 一种是二分+前缀和.复杂度O(nlogn).有点 ...
- POJ 3320 尺取法,Hash,map标记
1.POJ 3320 2.链接:http://poj.org/problem?id=3320 3.总结:尺取法,Hash,map标记 看书复习,p页书,一页有一个知识点,连续看求最少多少页看完所有知识 ...
- HDU 5358 尺取法+枚举
题意:给一个数列,按如下公式求和. 分析:场上做的时候,傻傻以为是线段树,也没想出题者为啥出log2,就是S(i,j) 的二进制表示的位数.只能说我做题依旧太死板,让求和就按规矩求和,多考虑一下就能发 ...
随机推荐
- ReactiveCocoa 谈谈RACMulticastConnection
本文出处:http://www.cnblogs.com/forkasi/p/4886740.html 在项目里,经常会使用这种方式创建一个signal 然后next RACSignal *four = ...
- css滚动条样式
1.横向滚动条:(abeamScroll) <div style="width:400px;height:200px;overflow-x:auto;overflow-y:hidden ...
- webService学习记录
服务端配置1.新建web项目,导入xFire的jar包2.创建服务接口.实现类3.创建src->META-INF->service->service.xml 将实现类.接口配置到里面 ...
- 怎么安装MySQL,安装MySQL遇到的一些问题!!!!!!
简介: 对于初学,我们在安装MySQL时,会出现各种各样的报错,这让我们非常的头痛.那么我来分享一下我在安装的过程中遇到的一些问题吧! 我们在安装MySQL之前,先安装好dotNetFx40_Clie ...
- MATLAB【工具箱下载】汇总
至于工具箱的安装说明参见:http://www.matlabsky.com/thread-120-1-1.html Maplesoft<Maple Toolbox for MATLAB> ...
- 【BZOJ3529】【莫比乌斯反演 + 树状数组】[Sdoi2014]数表
Description 有一张N×m的数表,其第i行第j列(1 < =i < =礼,1 < =j < =m)的数值为 能同时整除i和j的所有自然数之和.给定a,计算数表中不大于 ...
- Nginx中让 重写后的路径 自动增加斜线 /
http://www.111cn.net/sys/nginx/56067.htm(参考文章) 现在有个这样的需求,在重写的url地址后,自动加斜线 / 例如 xx.com/abc/1-2 (默认ur ...
- javascript 事件对象
1.事件对象 用来记录一些事件发生时的相关信息的对象 A.只有当事件发生的时候才产生,只能在处理函数内部访问 B.处理函数运行结束后自动销毁2.如何获取事件对象 IE: window.even ...
- linux正确重启MySQL的方法
修改了my.cnf,需要重启MySQL服务,正确重启MYSQL方法请看下面的文章 由于是从源码包安装的Mysql,所以系统中是没有红帽常用的servcie mysqld restart这个脚本 只好手 ...
- Python中的redis学习笔记
redis是一个key-value结构的数据库,value的格式可以使string,set,list,map(即python里面的dict),sorted set(有序集合) 1.初始化 1)直接连接 ...