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 a perfect power of prime p
{ L(x) otherwise L(2) = 1 * 2
L(3) = 1 * 2 * 3
L(4) = 1 * 2 * 3 * 2 // because 4 = 2^2
L(5) = 1 * 2 * 3 * 2 * 5
L(6) = 1 * 2 * 3 * 2 * 5 // 6 is not a perfect power of a prime
L(7) = 1 * 2 * 3 * 2 * 5 * 7
#include <bits/stdc++.h>
using namespace std;
typedef unsigned int UI;
const int maxn = 100000005;
const int N = 5800000;
UI mul[N];
int vis[maxn/32+10], p[N];
int cnt, n;
void init ()
{
cnt = 1;
p[0] = mul[0] = 2;
for (int i=3; i<maxn; i+=2)
if (!(vis[i/32]&(1<<((i/2)%16))))
{//寻找代表i的哪一位,偶数不占位数
p[cnt] = i;
mul[cnt] = mul[cnt-1] * i;
for (int j=3*i; j<maxn; j+=2*i)
vis[j/32] |= (1<<((j/2)%16));//删除有因子的位数
cnt ++;
}
//printf ("%d\n", cnt);
}
UI solve ()
{
int pos = upper_bound(p, p+cnt, n) - p - 1;//找出最大的比n小的素数
UI ans = mul[pos];
for (int i=0; i<cnt&&p[i]*p[i]<=n; i++)
{
int tem = p[i];
int tt = p[i] * p[i]; //这个tt很有可能溢出int
while (tt/tem == p[i]&&tt<=n)
{
tem *= p[i];
tt *= p[i];
}
ans *= tem / p[i];
}
return ans;
}
int main ()
{
int t, l = 0;
init ();
scanf ("%d", &t);
while (t --)
{
scanf ("%d", &n);
printf ("Case %d: %u\n", ++l, solve());
}
return 0;
}
LightOJ 1289 LCM from 1 to n(位图标记+素数筛的更多相关文章
- Light 1289 - LCM from 1 to n (位图标记+素数筛选)
题目链接: http://www.lightoj.com/volume_showproblem.php?problem=1289 题目描述: 给出一个n,求出lcm(1,2,3......n)为多少? ...
- LightOj 1289 - LCM from 1 to n(LCM + 素数)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1289 题意:求LCM(1, 2, 3, ... , n)%(1<<32), ...
- 1289 - LCM from 1 to n
http://blog.csdn.net/acdreamers/article/details/18507767 这个是位图的链接,这篇写的挺好. 模板: 1 #include<math.h&g ...
- LightOJ 1375 - LCM Extreme 莫比乌斯反演或欧拉扩展
题意:给出n [1,3*1e6] 求 并模2^64. 思路:先手写出算式 观察发现可以化成 那么关键在于如何求得i为1~n的lcm(i,n)之和.可以知道lcm(a,b)为ab/gcd(a,b) 变换 ...
- LightOJ 1341 - Aladdin and the Flying Carpet (唯一分解定理 + 素数筛选)
http://lightoj.com/volume_showproblem.php?problem=1341 Aladdin and the Flying Carpet Time Limit:3000 ...
- LightOJ 1074 Extended Traffic (最短路spfa+标记负环点)
Extended Traffic 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/O Description Dhaka city ...
- LightOJ 1197 Help Hanzo 素数筛
题意:筛一段区间内素数的个数,区间宽度10w,区间范围INT_MAX 分析:用sqrt(INT_MAX筛一遍即可),注意先筛下界,再筛上届,因为有可能包含 #include <cstdio> ...
- LightOJ - 1197 素数筛
深夜无事可干啊 #include<bits/stdc++.h> using namespace std; const int maxn = 1e6+11; typedef long lon ...
- lightoj 1028 - Trailing Zeroes (I)(素数筛)
We know what a base of a number is and what the properties are. For example, we use decimal number s ...
随机推荐
- uploadify加ASP.NET MVC3.0上传文件(可多条)
页面代码: <div id="fileQueuePlug"></div> <input type="file" name=&quo ...
- Oracle编程艺术--配置环境
如何设置login.sql,参照了本书作者的意见,我也大概弄明白了 只是该文件的存放位置一直就出错,百度了很久,说是$ORACLE_HOME/sqlplus/admin/glogin.sql(默认)于 ...
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_05 IO字符流_3_字符输出流_Writer类&FileWriter类
- GMSSL中生成SM2或RSA1024或RSA2048的证书相关命令
1.生成KEY:gmssl sm2 -genkey -out 01.root.pemgmssl genrsa -out 01.root_plain.key 2048gmssl genrsa -out ...
- POI向Excel中写入数据及追加数据
import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import ...
- POI读取指定Excel中行与列的数据
import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import ...
- 获取当前进程当前runtime加载的appdomain
using System.Runtime.InteropServices; // Add the following as a COM reference - C:\WINDOWS\Microsoft ...
- 从SOA 谈软件的发展
本文是个科普文章,有些内容可能不精准,为了给女儿解释SOA所写.要深刻理解SOA,必须了解软件的发展过程.互联网上有大量的这方面的文章可以参考. 软件与计算机 软件这个概念很年轻,也就不到70年. 软 ...
- Java 实现Excel的简单读取操作
JAVA实现Excel表单的简单读取操作 实现Excel表单的简单读取操作,首先要导入相关的jar包: 如图所示: 此处贴上代码: public static List<List<Stri ...
- oracle--对象权限
sys用户查询ww用户创建的表(已经commited) select * from ww.wwTable; 普通用户lisi查询ww用户的表 grant select on wwTable to li ...