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

POJ 3307 Smart Sister

时间:2015-09-02 21:58:02      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:

先找出所有的数,排序,然后o(1)效率询问

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
using namespace std;

long long Ans[1200000];
int tot;
map<long long, int>Cun;
const long long INF = 1e18;

void init()
{
    tot = 0;
    Cun.clear();
    Cun[1] = 1;
    Ans[tot] = 1;
}

int main()
{
    int head = 0;
    init();
    while (1)
    {
        if (Ans[head] == 0) break;
        long long  h = Ans[head];
        if (h * 2 > INF || Cun[h * 2] == 1) {}
        else
        {
            tot++;
            Ans[tot] = h * 2;
            Cun[h * 2] = 1;
        }

        if (h * 3 > INF || Cun[h * 3] == 1){}
        else
        {
            tot++;
            Ans[tot] = h * 3;
            Cun[h * 3] = 1;
        }

        if (h * 5 > INF || Cun[h * 5] == 1) {}
        else
        {
            tot++;
            Ans[tot] = h * 5;
            Cun[h * 5] = 1;
        }

        if (h * 7 > INF || Cun[h * 7] == 1) {}
        else
        {
            tot++;
            Ans[tot] = h * 7;
            Cun[h * 7] = 1;
        }
        head++;
    }
    sort(Ans, Ans + tot + 1);
    int T;
    scanf("%d", &T);
    while (T--)
    {
        int n;
        scanf("%d", &n);
        printf("%lld\n", Ans[n - 1]);
    }
    return 0;
}

 

POJ 3307 Smart Sister

标签:

原文地址:http://www.cnblogs.com/zufezzt/p/4779471.html

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