题目链接:

  http://www.lightoj.com/volume_showproblem.php?problem=1289

题目描述:

  给出一个n,求出lcm(1,2,3......n)为多少?

解题思路:

  lcm(1,2,3,......,n-1,n)等于所有小于n的素数p[i]的max(p[i]^k)相乘。

  暴力求解的话,由于时间的限制,很自然的想打了素数线性打标法,但是空间限制无法申请辣么大的标记数组。

  这个重要的时刻位图标记就闪亮登场啦!!!!

  int可以保存32位二进制,我们就可以把每一位当做一个数,又因为偶数除了二以外都不是素数,所以我们只需要筛选奇数。

 #include <bits/stdc++.h>
using namespace std;
typedef unsigned int UI;
const int maxn = ;
const int N = ;
UI mul[N];
int vis[maxn/+], p[N];
int cnt, n;
void init ()
{
cnt = ;
p[] = mul[] = ;
for (int i=; i<maxn; i+=)
if (!(vis[i/]&(<<((i/)%))))
{//寻找代表i的哪一位,偶数不占位数
p[cnt] = i;
mul[cnt] = mul[cnt-] * i;
for (int j=*i; j<maxn; j+=*i)
vis[j/] |= (<<((j/)%));//删除有因子的位数
cnt ++;
}
//printf ("%d\n", cnt);
}
UI solve ()
{
int pos = upper_bound(p, p+cnt, n) - p - ;//找出最大的比n小的素数
UI ans = mul[pos];
for (int i=; i<cnt&&p[i]*p[i]<=n; i++)
{
int tem = p[i];
int tt = p[i] * p[i];//这个tt很有可能溢出int(害的本宝宝wa了好几次)
while (tt/tem == p[i] && tt<=n)
{
tem *= p[i];
tt *= p[i];
}
ans *= tem / p[i];
}
return ans;
}
int main ()
{
int t, l = ;
init ();
scanf ("%d", &t);
while (t --)
{
scanf ("%d", &n);
printf ("Case %d: %u\n", ++l, solve());
}
return ;
}

Light 1289 - LCM from 1 to n (位图标记+素数筛选)的更多相关文章

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

    https://vjudge.net/contest/324284#problem/B 数学水题,其实就是想写下位图..和状压很像 题意:给n让求lcm(1,2,3,...,n),n<=1e8 ...

  2. 1289 - LCM from 1 to n

    http://blog.csdn.net/acdreamers/article/details/18507767 这个是位图的链接,这篇写的挺好. 模板: 1 #include<math.h&g ...

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

    http://lightoj.com/volume_showproblem.php?problem=1236 Pairs Forming LCM Time Limit:2000MS     Memor ...

  4. LightOj 1236 Pairs Forming LCM (素数筛选&&唯一分解定理)

    题目大意: 有一个数n,满足lcm(i,j)==n并且i<=j时,(i,j)有多少种情况? 解题思路: n可以表示为:n=p1^x1*p2^x1.....pk^xk. 假设lcm(a,b) == ...

  5. LightOj 1289 - LCM from 1 to n(LCM + 素数)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1289 题意:求LCM(1, 2, 3, ... , n)%(1<<32), ...

  6. BNU 12846 LCM Extreme 最小公倍数之和(线性欧拉筛选+递推)

    LCM Extreme Time Limit: 3000ms Memory Limit: 131072KB   This problem will be judged on UVALive. Orig ...

  7. 1341 - Aladdin and the Flying Carpet ---light oj (唯一分解定理+素数筛选)

    http://lightoj.com/volume_showproblem.php?problem=1341 题目大意: 给你矩形的面积(矩形的边长都是正整数),让你求最小的边大于等于b的矩形的个数. ...

  8. SPOJ LGLOVE 7488 LCM GCD Love (区间更新,预处理出LCM(1,2,...,n))

    题目连接:http://www.spoj.com/problems/LGLOVE/ 题意:给出n个初始序列a[1],a[2],...,a[n],b[i]表示LCM(1,2,3,...,a[i]),即1 ...

  9. 内存管理 初始化(二)bootmem位图分配器建立 及 使用

    本地的笔记有点长,先把bootmem位图分配器的建立 及  使用过程做下梳理. 都是代码,上面做了标注.开始的汇编部分省略了(涉及的内容不多,除了swapper_pg_dir的分配). 该记录不会再添 ...

随机推荐

  1. PHP_pear的安装和使用

    --------------            安装pear -------------- pear是PHP的扩展和应用程序库,包含了很多有用的类,安装好php5.0后,pear实际上并没有被安装 ...

  2. Mayor's posters-POJ2528(线段树+离散化)

    The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign h ...

  3. noip 2011

    铺地毯 题目描述 为了准备一个独特的颁奖典礼,组织者在会场的一片矩形区域(可看做是平面直角坐标系的第一象限)铺上一些矩形地毯.一共有 n 张地毯,编号从 1 到n .现在将这些地毯按照编号从小到大的顺 ...

  4. Builder设计模式

    Builder模式,又称生成器或构建者模式,属于对象创建型模式,侧重于一步一步的构建复杂对象,只有在构建完成后才会返回生成的对象.Builder模式将一个复杂对象的构建与它的表示分离,使得同样的构建过 ...

  5. JSP点击计数器

    以下内容引用自http://wiki.jikexueyuan.com/project/jsp/hits-counter.html: 一个点击计数器能得知关于网站某个特定页面的访问量.假设人们第一次登陆 ...

  6. 【SQL Server 学习系列】-- 收缩数据库文件大小

    USE WebExam; GO ALTER DATABASE WebExam SET RECOVERY SIMPLE; GO -- 收缩文件到 1 MB. ); GO ALTER DATABASE W ...

  7. 移动端 js 实现图片上传 预览

    方法一: <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv=&q ...

  8. android Qemu GPS 模块简明分析

    Android 的 gps module 是  gps.default.so 在system/lib/hw/ 文件夹上, 一般提供gps功能的手机应该实现这个module和真实gps硬件交互 Qemu ...

  9. 013 IPv6

    Router>en Router#config t Enter configuration commands, one per line.  End with CNTL/Z. Router(co ...

  10. 1072. Gas Station (30)【最短路dijkstra】——PAT (Advanced Level) Practise

    题目信息 1072. Gas Station (30) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B A gas station has to be built at s ...