hdu 2058 The sum problem

数学题目。折腾了一天,这种方法不是最好的,不过按照自己的第一直觉的方法AC还是很开心的。

 

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
struct abc{
    int start, end;
}dt[5000000];
bool cmp(const abc&a, const abc&b)
{
    if (a.start == b.start) return a.end < b.end;
    return a.start < b.start;
}
int main()
{
    int n, m, i;
    while (~scanf("%d%d", &n, &m))
    {
        if (n == 0 && m == 0) break;
        int summ = 0;
        int yy = sqrt(1.0*m);
        if (yy*yy < m) yy++;
        for (i = yy; i >= 1; i--)
        {
            if (m%i == 0)
            {
                int uu = i;
                int hh = m / i;

                if (uu == 1)
                {
                    if (m <= n)
                    {
                        dt[summ].start = hh;
                        dt[summ].end = hh;
                        summ++;
                    }
                    if ((hh - 1) % 2 == 0)
                    {
                        int x = (hh - 1) / 2;
                        if (x >= 1 && x <= n&&x + 1 >= 1 && x + 1 <= n)
                        {
                            dt[summ].start = x;
                            dt[summ].end = x + 1;
                            summ++;
                        }
                    }
                }
                else if (uu != 1)
                {
                    if (uu % 2 == 1)
                    {
                        int shu = uu / 2;
                        if (hh - shu >= 1 && hh - shu <= n&&hh + shu >= 1 && hh + shu <= n)
                        {
                            dt[summ].start = hh - shu;
                            dt[summ].end = hh + shu;
                            summ++;
                        }
                    }
                    if (hh % 2 == 1)
                    {
                        int xx = hh / 2;
                        if (xx - uu + 1 >= 1 && xx - uu + 1 <= n&&xx + uu >= 1 && xx + uu <= n)
                        {
                            dt[summ].start = xx - uu + 1;
                            dt[summ].end = xx + uu;
                            summ++;
                        }
                    }
                }
                uu = m / i; hh = i;
                if (uu == 1)
                {
                    if (m <= n)
                    {
                        dt[summ].start = hh;
                        dt[summ].end = hh;
                        summ++;
                    }
                    if ((hh - 1) % 2 == 0)
                    {
                        int x = (hh - 1) / 2;
                        if (x >= 1 && x <= n&&x + 1 >= 1 && x + 1 <= n)
                        {
                            dt[summ].start = x;
                            dt[summ].end = x + 1;
                            summ++;
                        }
                    }
                }
                else if (uu != 1)
                {
                    if (uu % 2 == 1)
                    {
                        int shu = uu / 2;
                        if (hh - shu >= 1 && hh - shu <= n&&hh + shu >= 1 && hh + shu <= n)
                        {
                            dt[summ].start = hh - shu;
                            dt[summ].end = hh + shu;
                            summ++;
                        }
                    }
                    if (hh % 2 == 1)
                    {
                        int xx = hh / 2;
                        if (xx - uu + 1 >= 1 && xx - uu + 1 <= n&&xx + uu >= 1 && xx + uu <= n)
                        {
                            dt[summ].start = xx - uu + 1;
                            dt[summ].end = xx + uu;
                            summ++;
                        }
                    }
                }
            }
        }
        sort(dt, dt + summ, cmp);
        for (i = 0; i < summ; i++)
        {
            if (i == 0) printf("[%d,%d]\n", dt[i].start, dt[i].end);
            else if (i>0)
            {
                if (dt[i].start != dt[i - 1].start || dt[i].end != dt[i - 1].end)
                    printf("[%d,%d]\n", dt[i].start, dt[i].end);
            }
            
        }
        printf("\n");
    }
    return 0;
}

 

posted @ 2015-05-02 18:45  Fighting_Heart  阅读(185)  评论(0编辑  收藏  举报