HDOJ1058解题报告

题目地址:

  http://acm.hdu.edu.cn/showproblem.php?pid=1058

题目概述:

  定义因子只有2,3,5,7的数为humble number,输入n,求出第n个humble number。规定1为第1个humble number。

大致思路:

  刚开始看到n小于5842时写的暴力被卡时间,强行打表被卡代码长度,丧心病狂优化暴力反而时间变长了23333

  最后忍不住上了STL,用priority_queue来维护所有humble number的序列,每次取出队列头的一个数,将这个数乘2,3,5,7之后push进队列中就可以了,不过需要注意去重。

  最后说一句,真的要好好学英文!!!我就是因为序数词的结尾写错了白白WA了3遍!!!

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cmath>
 5 #include <vector>
 6 #include <ctime>
 7 #include <map>
 8 #include <stack>
 9 #include <queue>
10 #include <cstring>
11 #include <algorithm>
12 using namespace std;
13
14 #define sacnf scanf
15 #define scnaf scanf
16 #define maxn  6000
17 #define maxm 26
18 #define inf 1061109567
19 #define Eps 0.00001
20 const double PI=acos(-1.0);
21 #define mod 1000033
22 #define MAXNUM 10000
23 void Swap(int &a,int &b) {int t=a;a=b;b=t;}
24 int Abs(int x) {return (x<0)?-x:x;}
25 typedef long long ll;
26 typedef unsigned int uint;
27
28 ll f[maxn];
29
30 bool judge(int x)
31 {
32     while(x%6==0) x/=6;
33     while(x%10==0) x/=10;
34     while(x%14==0) x/=14;
35     while(x%15==0) x/=15;
36     while(x%21==0) x/=21;
37     while(x%35==0) x/=35;
38     while(x%2==0) x/=2;
39     while(x%3==0) x/=3;
40     while(x%5==0) x/=5;
41     while(x%7==0) x/=7;
42     return (x==1)?true:false;
43 }
44
45 ll d[]={2,3,5,7};
46
47 void init()
48 {
49     int cnt=1;
50     priority_queue<ll,vector<ll>,greater<ll> > q;
51     q.push(1);
52     while(cnt<=5842)
53     {
54         ll x=q.top();q.pop();
55         if(f[cnt-1]==x) continue;
56         f[cnt++]=x;
57         for(int i=0;i<4;i++)
58         {
59             ll ans=x*d[i];
60             if(ans>0&&ans<=2000000000)
61             {
62                 q.push(ans);
63             }
64         }
65     }
66 }
67
68 int main()
69 {
70     //freopen("data.in","r",stdin);
71     //freopen("data.out","w",stdout);
72     //clock_t st=clock();
73     int n;init();
74     while(~scanf("%d",&n))
75     {
76         if(n==0) break;
77         printf("The %d",n);
78         if(n%10==1&&n%100!=11) printf("st");
79         else if(n%10==2&&n%100!=12) printf("nd");
80         else if(n%10==3&&n%100!=13) printf("rd");
81         else printf("th");
82         printf(" humble number is %lld.\n",f[n]);
83     }
84     //clock_t ed=clock();
85     //printf("\n\nTime Used : %.5lf Ms.\n",(double)(ed-st)/CLOCKS_PER_SEC);
86     return 0;
87 }
时间: 2024-07-31 14:29:26

HDOJ1058解题报告的相关文章

解题报告 之 POJ3057 Evacuation

解题报告 之 POJ3057 Evacuation Description Fires can be disastrous, especially when a fire breaks out in a room that is completely filled with people. Rooms usually have a couple of exits and emergency exits, but with everyone rushing out at the same time

hdu 1541 Stars 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541 题目意思:有 N 颗星星,每颗星星都有各自的等级.给出每颗星星的坐标(x, y),它的等级由所有比它低层(或者同层)的或者在它左手边的星星数决定.计算出每个等级(0 ~ n-1)的星星各有多少颗. 我只能说,题目换了一下就不会变通了,泪~~~~ 星星的分布是不是很像树状数组呢~~~没错,就是树状数组题来滴! 按照题目输入,当前星星与后面的星星没有关系.所以只要把 x 之前的横坐标加起来就可以了

【百度之星2014~初赛(第二轮)解题报告】Chess

声明 笔者最近意外的发现 笔者的个人网站http://tiankonguse.com/ 的很多文章被其它网站转载,但是转载时未声明文章来源或参考自 http://tiankonguse.com/ 网站,因此,笔者添加此条声明. 郑重声明:这篇记录<[百度之星2014~初赛(第二轮)解题报告]Chess>转载自 http://tiankonguse.com/ 的这条记录:http://tiankonguse.com/record/record.php?id=667 前言 最近要毕业了,有半年没做

2016 第七届蓝桥杯 c/c++ B组省赛真题及解题报告

2016 第七届蓝桥杯 c/c++ B组省赛真题及解题报告 勘误1:第6题第4个 if最后一个条件粗心写错了,答案应为1580. 条件应为abs(a[3]-a[7])!=1,宝宝心理苦啊.!感谢zzh童鞋的提醒. 勘误2:第7题在推断连通的时候条件写错了,后两个if条件中是应该是<=12 落了一个等于号.正确答案应为116. 1.煤球数目 有一堆煤球.堆成三角棱锥形.详细: 第一层放1个, 第二层3个(排列成三角形), 第三层6个(排列成三角形), 第四层10个(排列成三角形). -. 假设一共

[noip2011]铺地毯(carpet)解题报告

最近在写noip2011的题,备战noip,先给自己加个油! 下面是noip2011的试题和自己的解题报告,希望对大家有帮助,题目1如下 1.铺地毯(carpet.cpp/c/pas) [问题描述]为了准备一个独特的颁奖典礼,组织者在会场的一片矩形区域(可看做是平面直角坐标系的第一象限)铺上一些矩形地毯.一共有n 张地毯,编号从1 到n.现在将这些地毯按照编号从小到大的顺序平行于坐标轴先后铺设,后铺的地毯覆盖在前面已经铺好的地毯之上.地毯铺设完成后,组织者想知道覆盖地面某个点的最上面的那张地毯的

ACdream 1203 - KIDx&#39;s Triangle(解题报告)

KIDx's Triangle Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submit Statistic Next Problem Problem Description One day, KIDx solved a math problem for middle students in seconds! And than he created this problem. N

解题报告 之 CodeForces 91B Queue

解题报告 之 CodeForces 91B Queue Description There are n walruses standing in a queue in an airport. They are numbered starting from the queue's tail: the 1-st walrus stands at the end of the queue and the n-th walrus stands at the beginning of the queue.

解题报告 之 POJ1226 Substrings

解题报告 之 POJ1226 Substrings Description You are given a number of case-sensitive strings of alphabetic characters, find the largest string X, such that either X, or its inverse can be found as a substring of any of the given strings. Input The first li

解题报告 之 UVA563 Crimewave

解题报告 之 UVA563 Crimewave Description Nieuw Knollendam is a very modern town. This becomes clear already when looking at the layout of its map, which is just a rectangular grid of streets and avenues. Being an important trade centre, Nieuw Knollendam a