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取模 我们可以用unsigned int来存。

还有一个困难时筛求素数的时候我们开不出那么大的数组 ,因此我们需要用到一个新的工具

位图,就不在这里讲解了。

位图的讲解:http://dongxicheng.org/structure/bitmap/

用位图筛素数的方法:http://blog.csdn.net/raomeng1/article/details/8520199

代码如下:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;

const int maxn =100000009;
typedef unsigned int UUI;
int vis[maxn/32+50];
UUI mul[5800000];
int p[5800000],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)))){
            p[cnt]=i;
            mul[cnt]=mul[cnt-1]*i;
            cnt++;
            for(int j=i*3;j<maxn;j+=2*i)
                vis[j/32]|=(1<<((j/2)%16));
        }
    }
}

UUI solve()
{
    int pos=upper_bound(p,p+cnt,n)-p-1;//定位到第一个小于n的素数
    UUI ans = mul[pos];
    for(int i=0;i<cnt&&p[i]*p[i]<=n;i++){
        int tmp=p[i];
        int tt=p[i]*p[i];
        while(tt/tmp==p[i]&&tt<=n){//得到max(p[i]^k)<=n;用tmp*p[i]会爆int;
            tmp*=p[i];
            tt*=p[i];
        }
        ans*=(tmp/p[i]);
    }
    return ans;
}
int main()
{
    int t,cas=1;
    scanf("%d",&t);
    init();
    while(t--){
        scanf("%d",&n);
        printf ("Case %d: %u\n",cas++,solve());
    }
    return 0;
}
时间: 2024-07-29 19:39:08

LIGHT OJ 1289 LCM from 1 to n的相关文章

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 1341 Aladdin and the Flying Carpet

It's said that Aladdin had to solve seven mysteries before getting the Magical Lamp which summons a powerful Genie. Here we are concerned about the first mystery. Aladdin was about to enter to a magical cave, led by the evil sorcerer who disguised hi

light oj 1236 【大数分解】

给定一个大数,分解质因数,每个质因子的个数为e1,e2,e3,--em, 则结果为((1+2*e1)*(1+2*e2)--(1+2*em)+1)/2. //light oj 1236 大数分解素因子 #include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> #include <math.h> #include <ctype.h> #i

[2016-04-21][light]OJ[1234][Harmonic Number]

时间:2016-04-21 22:18:26 星期四 题目编号:[2016-04-21][light]OJ[1234][Harmonic Number] 题目大意:求∑nk=11kn∈(1,108),精确到10?8求∑k=1n1kn∈(1,108),精确到10?8 分析: 想法是打表,然后输出,但是直接打表会爆内存 解决办法,就是每隔100个来打表,节省1100的空间,然后从那个值开始计算到当前值解决办法,就是每隔100个来打表,节省1100的空间,然后从那个值开始计算到当前值 对应的整百就是n

Light OJ 1411 Rip Van Winkle`s Code 线段树成段更新

题目来源:Light OJ 1411 Rip Van Winkle`s Code 题意:3中操作 1种查询 求区间和 其中每次可以把一段区间从左到右加上1,2,3,...或者从右到左加上...3,2,1 或者把某个区间的数都置为v 思路:我是加了6个域 add是这段区间每个数都要加上add  add是这么来的 对与123456...这个等差数列 可能要分为2个区间 那么我就分成123和123 两个右边的等差数列每个数还应该加上3 所以右区间add加3 v是这个区间都要置为v 他的优先级最高 b是

Light OJ 1168 Wishing Snake 强连通缩点+哈密顿通路

题目来源:Light OJ 1168 Wishing Snake 题意:有点难看懂题意 看了一个小时再加别人的代码才懂意思 从0开始 输入的那些每一对u v 都要经过 就是从0到到达那些点 思路:首先缩点 每一个强连通分量里面的点都是可达的 缩点后的图是有向无环图 如果从0这个强连通分量可以出去到2个强连通分量 那么这两个强连通分量是不可能相互可达 所以可行的方案就是所有的强连通分量连成一线 一个一个串起来 除了第一个 出度是1入度是0和最后一个出度是0入度是1 其他都是入度等于出度是1 特判只

Jan&#39;s light oj 01--二分搜索篇

碰到的一般题型:1.准确值二分查找,或者三分查找(类似二次函数的模型). 2.与计算几何相结合答案精度要求比较高的二分查找,有时与圆有关系时需要用到反三角函数利用 角度解题. 3.不好直接求解的一类计数问题,利用二分直接枚举可能的结果,再检查是否符合题目要求. 4.区间求解,即利用两次二分分别查找有序序列左右上下限,再求差算出总个数. 题型知识补充: 1. 三分的一般写法: 1 double thfind(double left,double right) 2 { 3 double midmid

light oj 1422 - Halloween Costumes (区间dp)

1422 - Halloween Costumes PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Gappu has a very busy weekend ahead of him. Because, next weekend is Halloween, and he is planning to attend as many parties as he can. Since it's Ha

Light OJ 1114 Easily Readable 字典树

题目来源:Light OJ 1114 Easily Readable 题意:求一个句子有多少种组成方案 只要满足每个单词的首尾字符一样 中间顺序可以变化 思路:每个单词除了首尾 中间的字符排序 然后插入字典树 记录每个单词的数量 输入一个句子 每个单词也排序之后查找 根据乘法原理 答案就是每个单词的数量之积 #include <iostream> #include <cstring> #include <cstdio> #include <algorithm>