LightOJ 1289 LCM from 1 to n

1289 - LCM from 1 to n

Given an integer n, you have to find

lcm(1, 2, 3, ..., n)

lcm means least common multiple. For example lcm(2, 5, 4) = 20, lcm(3, 9) = 9, lcm(6, 8, 12) = 24.

Input

Input starts with an integer T (≤ 10000), denoting the number of test cases.

Each case starts with a line containing an integer n (2 ≤ n ≤ 108).

Output

For each case, print the case number and lcm(1, 2, 3, ..., n). As the result can be very big, print the result modulo 232.

Sample Input

Output for Sample Input


5

10

5

200

15

20


Case 1: 2520

Case 2: 60

Case 3: 2300527488

Case 4: 360360

Case 5: 232792560

Solution:

首先,lcm(1,2,...,n)为2k1*3k2*...*pxkx,pi为小于等于n的质数,piki<=n且pi(ki+1)>n

所以,先预处理出1e8范围内的质数,及其前缀积.

设pro[i]为∏p[j](1<=j<=i)

ans=1;
for(i=1;i<=31;++i)
{
  找到p[j]i<=n且p[j+1]i>n;
  ans*=pro[j];
}

Code:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<bitset>
using namespace std;
#define uint unsigned int
#define ull unsigned long long
const uint MAXN=1e8;
const uint MAXTOT=5761455;
const uint MAXPOW=31;
const uint INF=0xffffffff;
uint t,CASE;
bitset<MAXN+10> vis;
uint p[MAXTOT+10],tot;
uint pro[MAXTOT+10];
void GetP()
{
	for(uint i=2;i<=MAXN;++i)
	{
		if(!vis[i])p[++tot]=i;
		for(uint j=1;j<=tot&&(ull)p[j]*i<=MAXN;++j)
		{
			vis[p[j]*i]=true;
			if(i%p[j]==0)break;
		}
	}
}
uint QuickPow(uint x,uint y)
{
	ull res=1;
	while(y--)
	{
		res*=x;
		if(res>INF)return INF;
	}
	return res;
}
void init()
{
	GetP();
	pro[0]=1;
	for(uint i=1;i<=tot;++i)pro[i]=pro[i-1]*p[i];
}
uint n;
int main()
{
	init();
	scanf("%u",&t);
	while(t--)
	{
		scanf("%u",&n);
		uint res=1;
		for(uint i=1;i<=MAXPOW;++i)
		{
			uint left=0,mid,right=MAXTOT,r=0;
			while(left<=right)
			{
				mid=(left+right)>>1;
				if(QuickPow(p[mid],i)<=n)
				{
					r=mid;
					left=mid+1;
				}
				else right=mid-1;
			}
			res*=pro[r];
		}
		printf("Case %d: %u\n",++CASE,res);
	}
	return 0;
}
时间: 2024-07-29 19:39:01

LightOJ 1289 LCM from 1 to n的相关文章

LightOJ 1289 LCM from 1 to n(位图标记+素数筛

https://vjudge.net/contest/324284#problem/B 数学水题,其实就是想写下位图..和状压很像 题意:给n让求lcm(1,2,3,...,n),n<=1e8 思路:显然ans = 所有小于n的素数p[i]的max(p[i]^k)相乘.由于空间太大,装素数的数组开不下,要用位图,int可以保存32位二进制,我们可以把每一位当作一个数,又因为偶数除了2以外都不是素数,所以只需筛选奇数. L(1) = 1 L(x+1) = { L(x) * p if x+1 is

Light 1289 LCM from 1 to n 素数筛选位优化

题目来源:Light 1289 LCM from 1 to n 题意:.. 思路:从1到n 打过某个数是以一个素数的几次方 那么答案就乘以这个素数 主要是筛选素数 存不下 位优化 一个整数32位标记32个数 内存缩小32倍 是学习别人的 #include <cstdio> #include <cstring> #include <cstdio> #include <cmath> using namespace std; const int maxn = 10

LIGHT OJ 1289 LCM from 1 to n

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26999 题意: 给定一个n,求,LCM(1,2,3,.....,n) 分析: 几个数的最小公倍数等于这些数的素因子的最高次幂的乘积: 由于求1到n的所有的数的最小公倍数 ,所有的素因子为小于等于n的所有素数 因此我们至于要求出这些素数在n内的最高次幂即可. 我们可以先预处理出所有的素数的乘积,然后再乘上到n的p[i]最高次幂/p[i]. 因为是对2^32取模 我们

GCD&amp;&amp;LCM的一些经典问题

1.1~n的所有数的最小公倍数:lightoj 1289  传送门 分析:素因子分解可知这个数等于小于1~n的所有素数的最高次幂的乘积 预处理1~n的所有质数,空间较大,筛选的时候用位图来压缩,和1~n所有 质数的乘积,剩下的就是找最高次幂的问题了. 代码如下: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath>

LightOJ 1236 Pairs Forming LCM (LCM 唯一分解定理 + 素数筛选)

http://lightoj.com/volume_showproblem.php?problem=1236 Pairs Forming LCM Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1236 Description Find the result of the following code: long long pairs

Finding LCM LightOJ - 1215 (水题)

这题和这题一样......只不过多了个数... Finding LCM LightOJ - 1215 https://www.cnblogs.com/WTSRUVF/p/9316412.html #include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <set> #include <ve

LightOJ - 1215 Finding LCM

Finding LCM 已知a, b, c的最小公倍数为L, 给你a,b,问你是否存在最小的c满足题意,不存在输出impossible 素数分解 1 #include <cmath> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #define INF 0x3f3f3f3f 6 #define MOD 1000000007 7 using namespace std; 8

LightOJ 1236 - Pairs Forming LCM(素因子分解)

题意 给你一个数n 求满足lcm(a, b) == n, a <= b 的 (a,b) 的个数 容易知道 n 是a, b的所有素因子取在a, b中较大指数的积 先将n分解为素数指数积的形式 n = π(pi^ei) 那么对于每个素因子pi pi在a,b中的指数ai, bi 至少有一个等于pi, 另一个小于等于pi 先不考虑a, b的大小 对于每个素因子pi 1. 在a中的指数 ai == ei 那么 pi 在 b 中的指数可取 [0, ei] 中的所有数 有 ei + 1 种情况 2. 在a中的

LightOJ 1236 - Pairs Forming LCM (LCM&#183;唯一分解)

题意  给你一个数n  求满足lcm(a, b) == n, a <= b 的 (a,b) 的个数 容易知道 n 是a, b的所有素因子取在a, b中较大指数的积 先将n分解为素数指数积的形式  n = π(pi^ei)    那么对于每个素因子pi  pi在a,b中的指数ai, bi 至少有一个等于pi, 另一个小于等于pi 先不考虑a, b的大小  对于每个素因子pi 1. 在a中的指数 ai == ei   那么 pi 在 b 中的指数可取 [0, ei] 中的所有数  有 ei + 1