首页 > 代码库 > poj2100还是尺取

poj2100还是尺取

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 s 2 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 <= 10 14 ).

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.

Sample Input

2030

Sample Output

2
4 21 22 23 24
3 25 26 27
题意:给你一个数,求连续的整数的平方和为这个整数的个数
题解:尺取
技术分享
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007

using namespace std;

const int N=20000+5,maxn=10000+5,inf=0x3f3f3f3f;

ll ss[N],tt[N];

ll solve(ll x)//对x进行尺取
{
    ll ans=0,s=1,t=1,sum=0;
    while((t-2)*(t-2)<=x){//这里的范围一定要取好,刚开始取t<=x,结果tle了,然后取了t*t<=x,又wa了,因为t*t满足提议的时候会少算一种情况
        while(sum<x&&(t-2)*(t-2)<=x){
            sum+=(t*t);
            t++;
        }
    //    cout<<sum<<endl;
        if(sum<x)break;
        if(sum==x)
        {
            ss[ans]=s;
            tt[ans]=t;
            ans++;
        }
        sum-=(s*s);
        s++;
    }
    return ans;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll n,ans;
    cin>>n;
    ans=solve(n);
    cout<<ans<<endl;
    for(int i=0;i<ans;i++)
    {
        cout<<tt[i]-ss[i];
        for(int j=ss[i];j<tt[i];j++)
            cout<<" "<<j;
        cout<<endl;
    }
    return 0;
}
View Code

 

poj2100还是尺取