LightOJ 1289 LCM from 1 to n(位图标记+欧拉筛)

题目链接:https://vjudge.net/contest/373416#problem/A

位图标记法:https://blog.csdn.net/acdreamers/article/details/18507767

题意:要求是让求1到n的最小公倍数。

思路:https://blog.csdn.net/qq_42815188/article/details/104328129  

注:用二分查找节省时间,还有本题要求最终结果mod 2^32,可以借助 unsigned int 溢出时等价。

Sample Input

5

10

5

200

15

20

Sample Output

Case 1: 2520

Case 2: 60

Case 3: 2300527488

Case 4: 360360

Case 5: 232792560

代码如下:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<bitset>
#include<algorithm>
using namespace std;
const int N=1e8+10;
bitset<N>book;
int su[5800000];//素数
unsigned int sum[5800000];//前缀积
int r=0;
void prime()//线性筛打表
{
    for(int i=2; i<N; i++)
    {
        if(!book[i])
            su[r++]=i;
        for(int j=0; j<r&&i*su[j]<N; j++)
        {
            book[i*su[j]]=1;
            if(i%su[j]==0)
                break;
        }
    }

    sum[0]=2;
    for(int i=1; i<r; i++) //前缀积
        sum[i]=sum[i-1]*su[i];
}
unsigned int qq(int x,int y)//快速幂
{
    unsigned int res=1;
    while(y)
    {
        if(y%2)
            res*=x;
        x*=x;
        y/=2;
    }
    return res;
}
int main()
{
    prime();
    int t;
    int o=1;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        int id=upper_bound(su,su+r,n)-su;//找到第一个>n的素数位置
        id-=1;//找到最大的那个比n小的素数
        unsigned int ans=sum[id];
        for(int i=0; i<r&&su[i]*su[i]<=n; i++)
        {
            int xx=n;
            int num=0;
            while(xx/su[i])//求su[i]的最高次幂
            {
                xx/=su[i];
                num++;
            }
            num--;//前缀积里已经有一个了
            ans*=qq(su[i],num);
        }
        printf("Case %d: %u\n",o++,ans);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值