码迷,mamicode.com
首页 > 其他好文 > 详细

POJ2100 Graveyard Design(尺取法)

时间:2016-04-04 22:54:46      阅读:565      评论:0      收藏:0      [点我收藏+]

标签:

POJ2100 Graveyard Design

  题目大意:给定一个数n,求出一段连续的正整数的平方和等于n的方案数,并输出这些方案,注意输出格式;

    循环判断条件可以适当剪支,提高效率,(1^2+2^2+..n^2)=n*(n+1)*(2n+1)/6;

    尺取时一定要注意循环终止条件的判断。

    

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define in(n) scanf("%d",&(n))
#define in2(x1,x2) scanf("%d%d",&(x1),&(x2))
#define inll(n) scanf("%I64d",&(n))
#define inll2(x1,x2) scanf("%I64d%I64d",&(x1),&(x2))
#define inlld(n) scanf("%lld",&(n))
#define inlld2(x1,x2) scanf("%lld%lld",&(x1),&(x2))
#define inf(n) scanf("%f",&(n))
#define inf2(x1,x2) scanf("%f%f",&(x1),&(x2))
#define inlf(n) scanf("%lf",&(n))
#define inlf2(x1,x2) scanf("%lf%lf",&(x1),&(x2))
#define inc(str) scanf("%c",&(str))
#define ins(str) scanf("%s",(str))
#define out(x) printf("%d\n",(x))
#define out2(x1,x2) printf("%d %d\n",(x1),(x2))
#define outf(x) printf("%f\n",(x))
#define outlf(x) printf("%lf\n",(x))
#define outlf2(x1,x2) printf("%lf %lf\n",(x1),(x2));
#define outll(x) printf("%I64d\n",(x))
#define outlld(x) printf("%lld\n",(x))
#define outc(str) printf("%c\n",(str))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define mem(X,Y) memset(X,Y,sizeof(X));
typedef vector<int> vec;
typedef long long ll;
typedef pair<int,int> P;
const int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
const int INF=0x3f3f3f3f;
const ll mod=1e9+7;
ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
const bool AC=true;

struct node{
    ll num,a,b;
};
node p[100000];
bool cmp(node x,node y){
    return x.num>y.num;
}
int main(){
    ll n,s,t,sum,k,cnt;//都设为longlong免得溢出
    while(inll(n)!=EOF){
        s=t=1;sum=0;k=0;
        while(true){
            while(sum<n){  //注意循环终止条件
                sum+=t*t;
                t++;
            }
            if(sum==n) {
                p[k].num=t-s;
                p[k].a=s;
                p[k++].b=t-1;
            }
            sum-=s*s;
            s++;
            if(s*s>n) break; //注意循环终止条件
        }
        cnt=k;
        printf("%I64d\n",cnt);//别忘了这个
        sort(p,p+k,cmp);
        rep(i,0,k){
            printf("%I64d ",p[i].num);
            for(ll j=p[i].a;j<=p[i].b;j++){
                printf("%I64d ",j);
            }
            printf("\n");
        }
    }
    return 0;
}

 

POJ2100 Graveyard Design(尺取法)

标签:

原文地址:http://www.cnblogs.com/akrusher/p/5353125.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!