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 ...
随机推荐
- UNR#3 Day1——[ 堆+ST表+复杂度分析 ][ 结论 ][ 线段树合并 ]
地址:http://uoj.ac/contest/45 第一题是鸽子固定器. 只会10分.按 s 从小到大排序,然后 dp[ i ][ j ][ k ] 表示前 i 个元素.已经选了 j 个.最小值所 ...
- Layer Cake cf
Layer Cake time limit per test 6 seconds memory limit per test 512 megabytes input standard input ou ...
- Spring Boot 集成 JPA 的步骤
Spring Boot 集成 JPA 的步骤 配置依赖 compile group: 'org.springframework.boot', name: 'spring-boot-starter-da ...
- Wowza 4 vod 录播多层目录无法播放问题
找到一个解决方案,但是无法下载zip包 https://stackoverflow.com/questions/21303361/how-to-stream-mp4-files-from-subdir ...
- Hypermesh中弹簧单元设置
1D >> springs 单元类型 CBUSH1D 单元属性 PBUSH1D
- fedora23深度配置gnome系统环境, 如设置ibus的面板字体大小 以及gedit 自动探测文件字符编码fileencodings
除了系统桌面gnome, 以及gnome应用程序自带的preferences, 还有很多设置, 没有在preferences, 而是被深度地隐藏在系统中, 这时, 需要安装 dconf-tools: ...
- ceph-pve英语
adapted accordingly并相应地调整 silosn. 筒仓:粮仓:贮仓(silo的复数) saturatevt. 浸透,使湿透:使饱和,使充满While one HDD might no ...
- 红米note2 刷机 注意问题:
其他的百度都有,用刷线宝刷 红米note2 刷机 注意问题: 关机状态线下,链接电脑,按着音量下键不松手,按电源键开机后松开,即进入刷机模式. 其中,红米,红米1s移动,红米note移动3g/联通 ...
- Dnsmasq 安装配置
Dnsmasq 官网 http://www.thekelleys.org.uk/dnsmasq/doc.html Unbuntu 安装 Dnsmasq 参见https://help.ubuntu.c ...
- DataGridView数值列和日期列
本文转自:http://www.cnblogs.com/conexpress/p/5923324.html 在使用DataGridView编辑数据的时候,编辑的单元格一般会显示为文本框,逻辑值和图片会 ...