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

hdoj:2058

时间:2016-11-09 22:31:31      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:ack   str   nbsp   push   amp   ace   利用   node   turn   

 
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;

struct node{
    int left;
    int right;
};
int main()
{
    long  N, M;
    long long sum = 0;
    vector<node> nodes;
    while (cin >> N >> M)
    {
        if (N == 0 && M == 0)
            break;

        for (long n = N; n >=1; n--)
        {
            if ((2*M-n*n+n)%(2*n)!=0)
                continue;
            long long a1 = (2 * M - n*n + n) / (2 * n);
            
            long long an = a1 + n - 1;
            if (a1 >= 1 && a1 <= N && an >= 1 && an <= N && a1 <= an)
            {
                //node node;
                //node.left = a1;
                //node.right = an;
                //nodes.push_back(node);
                cout << "[" << a1 << "," << an << "]" << endl;
            }
        }
        //for (node node : nodes)
        //{
        //    cout << "[" << node.left << "," << node.right << "]" << endl;
        //}
        cout << endl;
    }
    return 0;
}

 

超时

 

 

a1 = k

an = k + n - 1

M = n*(2k+n-1)/2

解的

k = M/n - (n-1)/2,这个不能这样写,要写在一起

(2M-n*n-n)/(2*n)。否则对于30/4 - 3/2,会忽略这样的结果,如果先通分当然也就可以。

 超时

 

利用a1>=1这个条件可以减少循环次数

2M - n*n >n

2M > n*n - n>n*n

sqrt(2M)>n

#include <iostream>
#include <cmath>
#include <vector>
using namespace std;

struct node{
    int left;
    int right;
};
int main()
{
    long  N, M;
    long long sum = 0;
    vector<node> nodes;
    while (cin >> N >> M)
    {
        if (N == 0 && M == 0)
            break;

        for (long n = sqrt(2*M); n >=1; n--)
        {
            if ((2*M-n*n+n)%(2*n)!=0)
                continue;
            long a1 = (2 * M - n*n + n) / (2 * n);
            
            long an = a1 + n - 1;
            if (a1 >= 1 && a1 <= N && an >= 1 && an <= N && a1 <= an)
            {
                //node node;
                //node.left = a1;
                //node.right = an;
                //nodes.push_back(node);
                cout << "[" << a1 << "," << an << "]" << endl;
            }
        }
        //for (node node : nodes)
        //{
        //    cout << "[" << node.left << "," << node.right << "]" << endl;
        //}
        cout << endl;
    }
    return 0;
}

 

hdoj:2058

标签:ack   str   nbsp   push   amp   ace   利用   node   turn   

原文地址:http://www.cnblogs.com/theskulls/p/6048403.html

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