51nod 1010 只包含因子2 3 5的数 && poj - 1338 Ugly Numbers(打表)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1010
http://poj.org/problem?id=1338
首先poj这题要找出第n个丑数,想到要打表,因为每一个丑数都是由前一个丑数*2或者*3或者*5得到,每次取3种结果种较小的那一个放入数组中.
打表的时候要注意保持数组有序.
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
//#pragma comment(linker, "/STACK:102400000,102400000")
#define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0) #define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%I64d\n", x)
#define lowbit(x) (x)&(-x)
#define Read() freopen("a.txt", "r", stdin)
#define Write() freopen("b.txt", "w", stdout);
#define maxn 1010
#define maxv 1010
#define mod 1000000000
using namespace std;
ll p[];
int main()
{
//freopen("a.txt","r",stdin);
int i=,j=,k=;
p[]=;
for(int l=;l<=;l++)
{
p[l]=min(p[i]*,min(p[j]*,p[k]*));
if(p[l]==p[i]*) i++;
if(p[l]==p[j]*) j++;
if(p[l]==p[k]*) k++;
//printf("%lld\n",p[l]);
}
int n;
while(~scanf("%d",&n)&&n)
{
printf("%lld\n",p[n]);
}
return ;
}
51nod这题让你求>=n的丑数,因为n很大,所以打完表后需要二分查找,打表的时候要注意直到大于10^18,上限可以自己试几次.
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
//#pragma comment(linker, "/STACK:102400000,102400000")
#define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0) #define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%I64d\n", x)
#define lowbit(x) (x)&(-x)
#define Read() freopen("a.txt", "r", stdin)
#define Write() freopen("b.txt", "w", stdout);
#define maxn 1010
#define maxv 1010
#define mod 1000000000
using namespace std;
ll p[]; ll erfen(ll n)
{
int l=,r=;
while(l<=r)
{
int mid=(l+r)/;
if(p[mid]==n) return p[mid];
else if(p[mid]>n) r=mid-;
else l=mid+;
}
if(p[l]<n) l++;
return p[l];
}
int main()
{
freopen("a.txt","r",stdin);
int i=,j=,k=;
p[]=;
for(int l=;l<=;l++)
{
p[l]=min(p[i]*,min(p[j]*,p[k]*));
if(p[l]==p[i]*) i++;
if(p[l]==p[j]*) j++;
if(p[l]==p[k]*) k++;
//printf("%lld\n",p[l]);
}
//printf("%lld\n",p[10000]);
int t;
ll n;
scanf("%d",&t);
while(t--)
{
scanf("%lld",&n);
{
if(n==) printf("2\n");
else
printf("%lld\n",erfen(n));
}
}
return ;
}
51nod 1010 只包含因子2 3 5的数 && poj - 1338 Ugly Numbers(打表)的更多相关文章
- 51nod 1010 只包含因子2 3 5的数 二分答案
1010 只包含因子2 3 5的数 K的因子中只包含2 3 5.满足条件的前10个数是:2,3,4,5,6,8,9,10,12,15. 所有这样的K组成了一个序列S,现在给出一个数n,求S中 > ...
- 51nod 1010 只包含因子2 3 5的数 打表
只包含因子2 3 5的数 题目连接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1010 Description K的 ...
- 51Nod 1010 只包含因子2 3 5的数 Label:None
K的因子中只包含2 3 5.满足条件的前10个数是:2,3,4,5,6,8,9,10,12,15. 所有这样的K组成了一个序列S,现在给出一个数n,求S中 >= 给定数的最小的数. 例如:n = ...
- 51Nod 1010 只包含因子2 3 5的数
K的因子中只包含2 3 5.满足条件的前10个数是:2,3,4,5,6,8,9,10,12,15. 所有这样的K组成了一个序列S,现在给出一个数n,求S中 >= 给定数的最小的数. 例如:n = ...
- 51Nod 1010 只包含因子2 3 5的数(打表+二分)
K的因子中只包含2 3 5.满足条件的前10个数是:2,3,4,5,6,8,9,10,12,15. 所有这样的K组成了一个序列S,现在给出一个数n,求S中 >= 给定数的最小的数. 例如:n = ...
- 51Nod 1010 只包含因子2 3 5的数 | 预处理+二分
Input示例 5 1 8 13 35 77 Output示例 2 8 15 36 80 分析:将所有的只含有2 3 5因子的数打一个表保存在一个数组里,然后二分查找第一个>=数组里的数,输出 ...
- 1007 正整数分组 1010 只包含因子2 3 5的数 1014 X^2 Mod P 1024 矩阵中不重复的元素 1031 骨牌覆盖
1007 正整数分组 将一堆正整数分为2组,要求2组的和相差最小. 例如:1 2 3 4 5,将1 2 4分为1组,3 5分为1组,两组和相差1,是所有方案中相差最少的. Input 第1行:一个 ...
- 只包含因子2 3 5的数(51NOD 1010)
K的因子中只包含2 3 5.满足条件的前10个数是:2,3,4,5,6,8,9,10,12,15. 所有这样的K组成了一个序列S,现在给出一个数n,求S中 >= 给定数的最小的数. 例如:n = ...
- 把只包含因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> ...
随机推荐
- 关于springMVC传参问题
今天写项目,碰到一个以前灭有注意到的问题,一般情况下使用springMVC @Controller注解之后,被此注解标记的方法的参数名只需要跟页面表单的标签的name的值相同即可拿到页面的值,但是如果 ...
- html添加css——样式选择器
如何给html添加样式.两种方法: 一.新建立一个css样式表,与原html同目录,然后通过link标签链接.如:<link type="text/css" rel=&quo ...
- Java编程思想总结笔记Chapter 5
初始化和清理是涉及安全的两个问题.本章简单的介绍“垃圾回收器”及初始化知识. 第五章 初始化与清理 目录:5.1 用构造器确保初始化5.2 方法重载5.3 默认构造器5.4 this关键字5.5 清 ...
- [Android]AndroidDesign中ActionBar探究2 嵌入Fragment
上一节我们只是简单了介绍了Android Design风格中的ActionBar的简单实用,如添加MenuItem,这节我们会进一步了解ActionBar的其他功能. 在Android Develop ...
- iOS 蓝牙的GameKit用法
一.连接蓝牙 显示可以连接的蓝牙设备列表 - (IBAction)buildConnect:(id)sender { // 创建弹窗 GKPeerPickerController *ppc = [[G ...
- SQL Server中行列转置方法
PIVOT用于将列值旋转为列名(即行转列),在SQL Server 2000可以用聚合函数配合CASE语句实现 PIVOT的一般语法是:PIVOT(聚合函数(列) FOR 列 in (…) )AS P ...
- scrapy 请求传参
class MovieSpider(scrapy.Spider): name = 'movie' allowed_domains = ['www.id97.com'] start_urls = ['h ...
- close - 关闭一个文件描述符
SYNOPSIS 总览 #include <unistd.h> int close(int fd); DESCRIPTION 描述 close 关闭 一个 文件 描述符 , 使它 不在 指 ...
- java_线程的同步机制
1.同步代码块: 使用synchronized包住可能会出现安全问题的代码 import static java.lang.Thread.sleep; class Test01{ public sta ...
- hdfs深入:05、hdfs中的fsimage和edits的合并过程
6.4.secondarynameNode如何辅助管理FSImage与Edits文件 ①:secnonaryNN通知NameNode切换editlog ②:secondaryNN从NameNode中获 ...