51NOD 1434 区间LCM
来源:互联网 发布:重大电气知乎 编辑:程序博客网 时间:2024/06/05 00:48
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示例3123Output示例246孔炤 (题目提供者)
#include<stdio.h>#include <iostream>#include<stdlib.h>#include<algorithm>#include<vector>#include<deque>#include<map>#include<set>#include<queue>#include<math.h>#include<string.h>#include<string>using namespace std;#define ll long long#define pii pair<int,int>const int inf = 1e9 + 7;const int N = 1e6+5;vector<int>prim;bool isPrim[N];int num[N];void getPrimer(){ fill(isPrim+2,isPrim+N,true); for(int i=2,ed=sqrt(N+1);i<=ed;++i){ if(isPrim[i]){ for(ll j=(ll)i*i;j<N;j+=i){ isPrim[j]=false; } } } for(int i=2;i<N;++i){ if(isPrim[i]){ prim.push_back(i); } }}int getFactorNum(int x,int n){//x作为因子,在1-n中最多有多少 int ans=0; while(n>=x){ n/=x; ans+=1; } return ans;}int qPow(int x,int n){ int ans=1; int t=x; while(n){ if(n&1){ ans*=t; } t*=t; n>>=1; } return ans;}void getFactorNum(int n){//各素数因子 在1-n中最多有多少个 memset(num,0,sizeof(num)); for(int i=0;i<prim.size();++i){ int x=prim[i]; if(x>n){ break; } num[x]=getFactorNum(x,n); }}bool check(int n,int m){ for(int i=0;i<prim.size();++i){ int&x=prim[i]; if(x>n){ return true; } if(num[x]==0){ continue; } int tmp=qPow(x,num[x]); if(m/tmp==n/tmp){ return false; } } return true;}int binary_search(int n){ getFactorNum(n); int l=n+1,r=n*2; int ans=r; while(l<=r){ int mid=(l+r)/2; if(check(n,mid)){ ans=mid; r=mid-1; } else{ l=mid+1; } } return ans;}int main(){ //freopen("/home/lu/Documents/r.txt","r",stdin); //freopen("/home/lu/Documents/w.txt","w",stdout); int n,T; getPrimer(); scanf("%d",&T); while(T--){ scanf("%d",&n); printf("%d\n",binary_search(n)); } 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
- 利用CVX工具箱实现单快拍的稀疏矩阵DOA估计
- CG中的几何学——坐标系【2】
- c++ 设计模式6 (Decorator 装饰模式)
- android 解析json
- Android布局的基本概念及布局的运用?
- 51NOD 1434 区间LCM
- C语言再学习 -- 常用头文件和函数
- 《正则表达式》
- c++ 设计模式5 (Observer / Event 观察者模式)
- 《挑战程序设计竞赛》阅读笔记二 之 ALDS1_2_B Selection Sort
- 文章标题
- 安卓打开设置界面的方法
- springmvc源码1
- c++ 设计模式4 (Strategy)