POJ_2100_Graveyard_Design

描述


http://poj.org/problem?id=2100

求连续平方和=n的序列个数,并输出序列.

分析



直接尺取.

注意:

1.如果要用开根计算的话要写成 " ll ub=(ll)sqrt(n*1.9); "写成 " 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=1,r=0,k=0,len=0;
    ll sum=0;
    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=0;i<len;i++) printf("%lld ",fst+i);
    }
#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;
}
            
时间: 2024-10-10 13:38:43

POJ_2100_Graveyard_Design的相关文章