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 * 2L(3) = 1 * 2 * 3L(4) = 1 * 2 * 3 * 2      // because 4 = 2^2L(5) = 1 * 2 * 3 * 2 * 5L(6) = 1 * 2 * 3 * 2 * 5  // 6 is not a perfect power of a primeL(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;
}


原文地址:https://www.cnblogs.com/wzgg/p/11479902.html

时间: 2024-07-29 19:39:20

LightOJ 1289 LCM from 1 to n(位图标记+素数筛的相关文章

LightOJ 1289 LCM from 1 to n

1289 - LCM from 1 to n Given an integer n, you have to find lcm(1, 2, 3, ..., n) lcm means least common multiple. For example lcm(2, 5, 4) = 20, lcm(3, 9) = 9, lcm(6, 8, 12) = 24. Input Input starts with an integer T (≤ 10000), denoting the number of

Light 1289 LCM from 1 to n 素数筛选位优化

题目来源:Light 1289 LCM from 1 to n 题意:.. 思路:从1到n 打过某个数是以一个素数的几次方 那么答案就乘以这个素数 主要是筛选素数 存不下 位优化 一个整数32位标记32个数 内存缩小32倍 是学习别人的 #include <cstdio> #include <cstring> #include <cstdio> #include <cmath> using namespace std; const int maxn = 10

LIGHT OJ 1289 LCM from 1 to n

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26999 题意: 给定一个n,求,LCM(1,2,3,.....,n) 分析: 几个数的最小公倍数等于这些数的素因子的最高次幂的乘积: 由于求1到n的所有的数的最小公倍数 ,所有的素因子为小于等于n的所有素数 因此我们至于要求出这些素数在n内的最高次幂即可. 我们可以先预处理出所有的素数的乘积,然后再乘上到n的p[i]最高次幂/p[i]. 因为是对2^32取模 我们

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

http://lightoj.com/volume_showproblem.php?problem=1341 Aladdin and the Flying Carpet Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1341 Description It's said that Aladdin had to solve seven

51NOD 1434 区间LCM(素数筛)

传送门 1434 区间LCM 题目来源: TopCoder 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 一个整数序列S的LCM(最小公倍数)是指最小的正整数X使得它是序列S中所有元素的倍数,那么LCM(S)=X. 例如,LCM(2)=2,LCM(4,6)=12,LCM(1,2,3,4,5)=60. 现在给定一个整数N(1<=N<=1000000),需要找到一个整数M,满足M>N,同时LCM(1,2,3,4,-,N-1,N) 整除 LCM

LightOJ Bi-shoe and Phi-shoe 1370【欧拉函数+素数打表】

1370 - Bi-shoe and Phi-shoe PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Bamboo Pole-vault is a massively popular sport in Xzhiland. And Master Phi-shoe is a very popular coach for his success. He needs some bamboos for

LightOJ 1197(区间素数筛)

Help Hanzo Amakusa, the evil spiritual leader has captured the beautiful princess Nakururu. The reason behind this is he had a little problem with Hanzo Hattori, the best ninja and the love of Nakururu. After hearing the news Hanzo got extremely angr

GCD&amp;&amp;LCM的一些经典问题

1.1~n的所有数的最小公倍数:lightoj 1289  传送门 分析:素因子分解可知这个数等于小于1~n的所有素数的最高次幂的乘积 预处理1~n的所有质数,空间较大,筛选的时候用位图来压缩,和1~n所有 质数的乘积,剩下的就是找最高次幂的问题了. 代码如下: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath>

light_oj 1236 求最小公倍数( lcm(a,b) )等于n的数对 素因数分解

light_oj 1236 求最小公倍数( lcm(a,b) )等于n的数对  素因数分解 H - Pairs Forming LCM Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1236 Description Find the result of the following code: long long pairsFormL