poj 2566

来源:互联网 发布:网络博客网站 编辑:程序博客网 时间:2024/06/12 01:44
这幅图便是尺取法怎么“取”的过程了。

  整个过程分为4布:

    1.初始化左右端点

    2.不断扩大右端点,直到满足条件

    3.如果第二步中无法满足条件,则终止,否则更新结果

    4.将左端点扩大1,然后回到第二步http://images.cnitblog.com/blog/597004/201408/291224259702079.jpg


#include <iostream>#include <cstdio>#include <algorithm>#include <vector>#define LL long longusing namespace std;vector<pair<LL, LL> > ans;void solve(LL n){    ans.clear();    LL s = 1, t = 1;    LL sum = 0;    while (s * s <= n)    {        while (t * t <= n && sum < n)        {            sum += t * t;            t++;        }        if (sum == n)        {            ans.push_back(make_pair(s, t));        }        sum -= s * s;        s++;    }    int sz = ans.size();    printf("%d\n", sz);    for (int i = 0; i < sz; i++)    {        printf("%d", ans[i].second - ans[i].first);        for (int j = ans[i].first; j < ans[i].second; j++)        {            printf(" %d", j);        }        puts("");    }}int main(){    LL n;    while (~scanf("%lld", &n))    {        solve(n);    }    return 0;}

0 0
原创粉丝点击