51nod 1010《只包含因子2 3 5的数》
2018-04-07 23:28
253 查看
1010 只包含因子2 3 5的数
![](http://file.51nod.com/images/icon/ok.png)
基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题
![](http://file.51nod.com/images/icon/star.png)
收藏
![](http://file.51nod.com/images/icon/plus.png)
关注K的因子中只包含2 3 5。满足条件的前10个数是:2,3,4,5,6,8,9,10,12,15。所有这样的K组成了一个序列S,现在给出一个数n,求S中 >= 给定数的最小的数。例如:n = 13,S中 >= 13的最小的数是15,所以输出15。Input
题解:数据很大,需要先打表,利用了stl的函数,二分查找以及排序
lower_bound()返回一个 iterator 它指向在[first,last)标记的有序序列中可以插入value,而不会破坏容器顺序的第一个位置,而这个位置标记了一个大于等于value 的值。该函数为C++ STL内的函数。
![](http://file.51nod.com/images/icon/ok.png)
基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题
![](http://file.51nod.com/images/icon/star.png)
收藏
![](http://file.51nod.com/images/icon/plus.png)
关注K的因子中只包含2 3 5。满足条件的前10个数是:2,3,4,5,6,8,9,10,12,15。所有这样的K组成了一个序列S,现在给出一个数n,求S中 >= 给定数的最小的数。例如:n = 13,S中 >= 13的最小的数是15,所以输出15。Input
第1行:一个数T,表示后面用作输入测试的数的数量。(1 <= T <= 10000) 第2 - T + 1行:每行1个数N(1 <= N <= 10^18)Output
共T行,每行1个数,输出>= n的最小的只包含因子2 3 5的数。Input示例
5 1 8 13 35 77Output示例
2 8 15 36
80
题解:数据很大,需要先打表,利用了stl的函数,二分查找以及排序
lower_bound()返回一个 iterator 它指向在[first,last)标记的有序序列中可以插入value,而不会破坏容器顺序的第一个位置,而这个位置标记了一个大于等于value 的值。该函数为C++ STL内的函数。
#include<stdio.h> #include<algorithm> using namespace std; typedef long long ll; ll dp[100010]; ll Max=1e18; int num=0; void init() { for(ll i=1;i<=Max;i*=2) for(ll j=1;j*i<=Max;j*=3) for(ll k=1;k*j*i<=Max;k*=5) { dp[num++]=k*j*i;//存储只含2,3,5,因子的数 } } int main() { int t; init(); sort(dp,dp+num);//需要排序 scanf("%d",&t); while(t--) { ll n; scanf("%lld",&n);//lower_bound返回的是一个位置 printf("%lld\n",*lower_bound(dp+1,dp+num,n));//一不是答案 } return 0; }
相关文章推荐
- 51nod 1010 只包含因子2 3 5的数 打表二分
- 51nod 1010 只包含因子2 3 5的数 【容器 +二分 】
- 51nod-1010 只包含因子2 3 5的数
- 51nod 1010 只包含因子2 3 5的数 (打表+二分)
- 51nod 1010 只包含因子2 3 5的数【打表+二分】
- 51Nod 1010 只包含因子2 3 5的数
- 51Nod 1010 只包含因子2 3 5的数
- 【51nod】--1010 只包含因子2 3 5的数 (二分&&数论&&打表)
- 51nod 1010 只包含因子2 3 5的数(预处理+二分)
- 51nod 1010 只包含因子2 3 5的数 打表
- 51Nod-1010 只包含因子2 3 5的数【打表+排序+二分搜索】
- 51nod--1010 只包含因子2 3 5的数
- 51Nod--1010 只包含因子2 3 5的数
- 51NOD 1010 只包含因子2 3 5的数(二分 + 预处理)
- 51Nod 1010 只包含因子2 3 5的数 打表+二分
- 51Nod 1010 只包含因子2 3 5的数(打表+二分)
- 51nod 1010 只包含因子2 3 5的数
- 51nod 1010 只包含因子2 3 5的数
- 51nod 1010 只包含因子2 3 5的数
- 51nod 1010 只包含因子2 3 5的数