51nod-1434 区间LCM
来源:互联网 发布:淘宝引流宝怎么设置 编辑:程序博客网 时间:2024/05/18 05:07
原题链接
1434 区间LCM
题目来源: TopCoder
基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题
收藏
关注
一个整数序列S的LCM(最小公倍数)是指最小的正整数X使得它是序列S中所有元素的倍数,那么LCM(S)=X。
例如,LCM(2)=2,LCM(4,6)=12,LCM(1,2,3,4,5)=60。
现在给定一个整数N(1<=N<=1000000),需要找到一个整数M,满足M>N,同时LCM(1,2,3,4,...,N-1,N) 整除 LCM(N+1,N+2,....,M-1,M),即LCM(N+1,N+2,....,M-1,M)是LCM(1,2,3,4,...,N-1,N) 的倍数.求最小的M值。
Input
多组测试数据,第一行一个整数T,表示测试数据数量,1<=T<=5每组测试数据有相同的结构构成:每组数据一行一个整数N,1<=N<=1000000。
Output
每组数据一行输出,即M的最小值。
Input示例
3123
Output示例
246
只要1到n中每个质因子的指数最高次,在n+1, m中也出现即可
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <vector>#define maxn 1000000using namespace std;typedef long long ll;int cnt;int prime[maxn+5], p[maxn+5];void Prime(){for(int i = 2; i <= maxn; i++){if(prime[i] == 0){p[cnt++] = i;for(ll j = (ll)i * i; j <= maxn; j += i){prime[j] = 1;}}}}int main(){//freopen("in.txt", "r", stdin);int t;Prime();scanf("%d", &t);while(t--){ll n;scanf("%I64d", &n);int ans = 2;for(int i = 0; i < cnt && p[i] <= n; i++){ll sum = 1;while(sum * p[i] <= n) sum *= p[i];ans = max((ll)ans, (n / sum + 1) * sum);}printf("%d\n", ans);} return 0;}
0 0
- 51nod 1434 区间LCM
- 51nod-1434 区间LCM
- 51nod 1434 区间LCM
- 51NOD 1434 区间LCM
- 51nod-1434 区间LCM
- 51nod 1434 区间LCM
- 51nod 1434 数论区间LCM问题
- 51Nod-1434-区间LCM
- 51nod 1434 区间LCM【数论】
- 51NOD 1434 区间LCM(素数筛)
- 1434 区间LCM
- 1434 区间LCM
- 【 51NOD 1434 素数筛 】【数论+思维+筛素数】区间LCM【找到一个最小整数M,满足M>N,LCM(N+1,N+2,..,M-1,M)是LCM(1,2,3,4,.,N-1,N) 的倍数】
- 51nod 1012 最小公倍数LCM
- 51 NOD 1012 最小公倍数LCM
- 【51Nod】1012 最小公倍数LCM
- 51Nod-1012-最小公倍数LCM
- 51nod 1012 最小公倍数LCM
- 行程问题
- 表达式求值
- 子对象构造函数和析构函数的调用顺序
- java反射
- 让Magento 新上传的产品在分类显示最前面?
- 51nod-1434 区间LCM
- hdu 1005 Number Sequence
- 流水行船问题
- java类加载机制
- Android MVP
- 【50.26%】【hdu 5907】Find Q
- 使用HttpClient4.3.1模拟Http请求与无信任证书访问Https
- 二叉树的三种遍历
- 对TCP端口和连接数的一点思考