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) 的二进制表示的位数.只能说我做题依旧太死板,让求和就按规矩求和,多考虑一下就能发 ...
随机推荐
- LINQ里的Distinct()
IQueryable 继承自IEnumerable 先举例: #region linq to object List<People> peopleList = new List<Pe ...
- 关于ibatis进行物理游标分页
http://www.iteye.com/topic/136712 详细demo:参照http://www.kusoft.net 我的数据库是采用mssql2000 采用分页必定数据量比较大: 按照i ...
- linux安装rzsz
rz,sz是Linux/Unix同Windows进行ZModem文件传输的命令行工具优点:比ftp命令方便,而且服务器不用打开FTP服务. sz:将选定的文件发送(send)到本地机器rz:运行该命令 ...
- CWnd::UpdateData
CWnd::UpdateData 格式: BOOL UpdateData( BOOL bSaveAndValidate = TRUE ); 描述:调用该成员函数初始化在对话框中的数据,或检索和验证对话 ...
- websocket++简单使用例子
前言 html5支持使用websocket协议与服务器保持一个长连接,方便双方互相传输数据,而且服务器也能主动发送信息给客户端,而在这之前使用HTTP是很难做到的.下面介绍使用C++实现的websoc ...
- 静态库的pdb
静态库也会有pdb,只不过,默认是以vc编译器作为名字,比如vs2003的lib的pdb是vc70.pdb,vs2008则是vc90.pdb
- centos 6.5 安装jenkins
Installation sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.rep ...
- php 文件上传的基本方法
基本思路:1.HTML表单中 form中的enctype必为enctype="multipart/form-data",method = post 设置提交数据中的type = f ...
- React组件三
<script> <!-- getDefalutPros 设置组件的默认值--> <!--var Mytitle=React.createClass({ getDefau ...
- a标签拨打电话、发短信、发邮件
1.<a href=”tel:xxxx”>拨打电话</a> 2.<a href=”sms:xxxx”>发短信</a> 3.<a href=”mai ...