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 ...
随机推荐
- 设计模式学习笔记——Visitor 访问者模式
1.定义IVisitor接口,确定变化所涉及的方法 2.封装变化类.实现IVisitor接口 3.在实体类的变化方法中传入IVisitor接口,由接口确定使用哪一种变化来实现(封装变化) 4.在使用时 ...
- hdu3714 Error Curves
题目: Error Curves Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- 大数据笔记(六)——HDFS的底层原理:JAVA动态代理和RPC
一.Java的动态代理对象 实现代码如下: 1.接口类MyService package hdfs.proxy; public interface MyService { public void me ...
- uva live 7635 National Bomb Defusing Squad
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- Git 创建版本库并实现本地上传数据到GitHub库
版本库又叫做仓库,其实也是一个目录,这个目录里的所有文件都是被Git管理着,对每个文件的修改,删除,Git都会进行记录,方便我们对其进行跟踪. 因为本地是window环境,我们先从官网下载好windo ...
- Python编程:从入门到实践—变量和简单数据类型
变量的命名和使用 #!/usr/bin/env python# -*- encoding:utf-8 -*- message ="Hello Python world!"print ...
- 红帽虚拟化RHEV-PXE批量安装RHEV-H
目录 目录 前言 软件环境 前提 部署 PXE 环境 使用 yum 安装所需软件 配置 DHCP 配置 TFTP-Server 配置 vsftpd 服务用于提供安装系统所需软件包 安装 kicksta ...
- 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第8节 Math类_19_Math练习:小学数学真题
题目 画数轴 解题思路 强转成int类型就会舍弃小数位数 输出最终的数量 如果用Math.ceil的方式的话
- 07 oracle 归档模式 inactive/current redo log损坏修复--以及错误ORA-00600: internal error code, arguments: [2663], [0], [9710724], [0], [9711142], [], [], [], [], [], [], []
07 oracle 归档模式 inactive/current redo log损坏修复--以及错误ORA-00600: internal error code, arguments: [2663], ...
- python基础数据类型补充以及编码的进阶
一.基本数据类型的补充循环列表改变列表大小的问题#请把列表中索引为基数的元素写出l1=[1,2,3,4,5,6]for i in l1: if i%2!=0: print(i)结果:135二:基本数据 ...