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(位图标记+素数筛的更多相关文章

  1. Light 1289 - LCM from 1 to n (位图标记+素数筛选)

    题目链接: http://www.lightoj.com/volume_showproblem.php?problem=1289 题目描述: 给出一个n,求出lcm(1,2,3......n)为多少? ...

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

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

  3. 1289 - LCM from 1 to n

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

  4. LightOJ 1375 - LCM Extreme 莫比乌斯反演或欧拉扩展

    题意:给出n [1,3*1e6] 求 并模2^64. 思路:先手写出算式 观察发现可以化成 那么关键在于如何求得i为1~n的lcm(i,n)之和.可以知道lcm(a,b)为ab/gcd(a,b) 变换 ...

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

    http://lightoj.com/volume_showproblem.php?problem=1341 Aladdin and the Flying Carpet Time Limit:3000 ...

  6. LightOJ 1074 Extended Traffic (最短路spfa+标记负环点)

    Extended Traffic 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/O Description Dhaka city ...

  7. LightOJ 1197 Help Hanzo 素数筛

    题意:筛一段区间内素数的个数,区间宽度10w,区间范围INT_MAX 分析:用sqrt(INT_MAX筛一遍即可),注意先筛下界,再筛上届,因为有可能包含 #include <cstdio> ...

  8. LightOJ - 1197 素数筛

    深夜无事可干啊 #include<bits/stdc++.h> using namespace std; const int maxn = 1e6+11; typedef long lon ...

  9. 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 ...

随机推荐

  1. 使用 Markdown 写博客

    后台设置(左侧边栏区找到-设置默认编辑器). 设置为 Markdown 后保存,即可在编辑新博客时生效.

  2. 【l转】VS2015下解决:无法解析的外部符号 __imp___vsnprintf 及__iob_func

    https://blog.csdn.net/hebbely/article/details/53780562

  3. Ubuntu 系统搭建LNMP环境

    当前Linux版本:Ubuntu16.04 一.安装Nginx 在终端中输入命令 " sudo apt-get install nginx ",在确认安装完成后,在浏览器中访问 l ...

  4. 关闭掉mysql 8和mysql5.7的密码验证插件validate_password

    在mysql文档中的一段话If you installed MySQL 5.7 using the MySQL Yum repository, MySQL SLES Repository, or RP ...

  5. 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_05 IO字符流_1_字符输入流_Reader类&FileRead

  6. Numpy的补充(重要!!)

    轴的概念 英文解释  https://www.sharpsightlabs.com/blog/numpy-axes-explained/ 汉化解释 https://www.jianshu.com/p/ ...

  7. 网络编程之urllib

    #网络爬虫,从其他的网站上,获取一些有用的内容,存入自己的数据库,然后再展示在指定的位置.#urllib是python自带的模块 1.urllib模块做网络爬虫,爬取网页: from urllib i ...

  8. http://blog.csdn.net/sdksdk0/article/details/50749326

    http://blog.csdn.net/sdksdk0/article/details/50749326

  9. Week3 - 397. Integer Replacement

    Week3 - 397. Integer Replacement 397.Integer Replacement - Medium Given a positive integer n and you ...

  10. python实现excel转换成pdf

    1.安装 需要安装pywin32包,以实现对Office文件的操作,可以批量转换为pdf文件.支持 doc, docx, ppt, pptx, xls, xlsx 等格式. pip install p ...