题目链接:http://lightoj.com/volume_showproblem.php?problem=1289

题意:求LCM(1, 2, 3, ... , n)%(1<<32), (1<n<=1e8);

LCM(1, 2, 3, ... , n) = n以内所有素数的最高次幂之积,例如15: 23*32*5*7*11*13 = 36360360;

为了防止TLE所以,要有一个数组表示前缀积,但是直接开LL会MLE是,因为有个%1<<32刚好是unsigned int之内,可以开int的数组;

关于求1e8内的素数表,用bool类型也会MLE的,所以可以使用bitset类型;

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <bitset>
#include <iostream>
#include <time.h> typedef long long LL; using namespace std; const int N = 1e8+;
const double eps = 1e-;
const int INF = 0x3f3f3f3f;
const LL mod = (1ll<<); int k, p[];
unsigned int Mul[];
bitset<N> f; void Init()
{
f.reset();
for(int i=; i<N; i++)
{
if(f[i]) continue;
p[k++] = i;
for(int j=i+i; j<N; j+=i)
f[j] = ;
}
} int main()
{
int T, t = ;
scanf("%d", &T); Init(); Mul[] = p[];
for(int i=; i<k; i++)
Mul[i] = Mul[i-]*p[i]; while(T --)
{
int n; scanf("%d", &n); int pos = upper_bound(p, p+k, n)-p - ; LL ans = Mul[pos]; for(int i=; i<k && (LL)p[i]*p[i]<=n; i++)
{
LL num = ;
while(num <= n)
num *= p[i];
if(num%(p[i]*p[i]) == ) num /= (p[i]*p[i]);
ans = ans*num%mod;
} printf("Case %d: %lld\n", t++, ans);
}
return ;
}

还有一种比较快一点的方法:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <bitset>
#include <iostream>
#include <time.h> typedef long long LL; using namespace std; const int N = 1e8+;
const double eps = 1e-;
const int INF = 0x3f3f3f3f;
const LL mod = (1ll<<); int k = , p[], f[N/+];
unsigned int Mul[]; void Init()
{
p[k++] = ;
for(int i=; i<N; i+=)
{
if(f[i/]&(<<((i/)%)))
continue;
p[k++] = i;
for(int j=*i; j<N; j+=*i)
f[j/] |= (<<((j/)%));
}
///printf("%d\n", k);
} int main()
{
int T, t = ;
scanf("%d", &T); Init(); Mul[] = p[];
for(int i=; i<k; i++)
Mul[i] = Mul[i-]*p[i]; while(T --)
{
int n; scanf("%d", &n); int pos = upper_bound(p, p+k, n)-p - ; LL ans = Mul[pos]; for(int i=; i<k && (LL)p[i]*p[i]<=n; i++)
{
LL num = ;
while(num <= n)
num *= p[i];
if(num%(p[i]*p[i]) == ) num /= (p[i]*p[i]);
ans = ans*num%mod;
} printf("Case %d: %lld\n", t++, ans);
}
return ;
}

LightOj 1289 - LCM from 1 to n(LCM + 素数)的更多相关文章

  1. LightOJ 1289 LCM from 1 to n(位图标记+素数筛

    https://vjudge.net/contest/324284#problem/B 数学水题,其实就是想写下位图..和状压很像 题意:给n让求lcm(1,2,3,...,n),n<=1e8 ...

  2. HDU 1019 Least Common Multiple【gcd+lcm+水+多个数的lcm】

    Least Common Multiple Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot ...

  3. POJ-2429 GCD & LCM Inverse---给出gcd和lcm求原来两个数

    题目链接: https://cn.vjudge.net/problem/POJ-2429 题目大意: 给出两个数的gcd和lcm,求原来的这两个数(限定两数之和最小). 解题思路: 首先,知道gcd和 ...

  4. LightOJ 1236 - Pairs Forming LCM(素因子分解)

    B - Pairs Forming LCM Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  5. LightOJ 1236 Pairs Forming LCM (LCM 唯一分解定理 + 素数筛选)

    http://lightoj.com/volume_showproblem.php?problem=1236 Pairs Forming LCM Time Limit:2000MS     Memor ...

  6. Pairs Forming LCM (LightOJ - 1236)【简单数论】【质因数分解】【算术基本定理】(未完成)

    Pairs Forming LCM (LightOJ - 1236)[简单数论][质因数分解][算术基本定理](未完成) 标签: 入门讲座题解 数论 题目描述 Find the result of t ...

  7. 1289 - LCM from 1 to n

    http://blog.csdn.net/acdreamers/article/details/18507767 这个是位图的链接,这篇写的挺好. 模板: 1 #include<math.h&g ...

  8. Pairs Forming LCM LightOJ - 1236 素因子分解

    Find the result of the following code: long long pairsFormLCM( int n ) {    long long res = 0;    fo ...

  9. LightOJ - 1236 - Pairs Forming LCM(唯一分解定理)

    链接: https://vjudge.net/problem/LightOJ-1236 题意: Find the result of the following code: long long pai ...

随机推荐

  1. BZOJ2061 : Country

    记忆化搜索,设$f[i][j]$表示符号$i$一开始kmp指针为$j$,中间匹配了多少次,$g[i][j]$则表示匹配结束后kmp指针的位置. 时间复杂度$O(nl^2)$. #include< ...

  2. ACM: poj 1094 Sorting It All Out - 拓扑排序

    poj 1094 Sorting It All Out Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & ...

  3. 【BZOJ】3211: 花神游历各国

    题意 \(n\)个点,第\(i\)个点值为\(a_i\).\(m\)个询问,每次询问\([l, r]\)内的和或者将\([l, r]\)的每个值改为自己的算术平方根.(\(n \le 100000, ...

  4. Codeforces Round #210 (Div. 2) C. Levko and Array Recovery

    题目链接 线段树的逆过程,想了老一会,然后发现应该是包含区间对存在有影响,就不知怎么做了...然后尚大神,说,So easy,你要倒着来,然后再正着来,判断是不是合法就行了.然后我乱写了写,就过了.数 ...

  5. POJ 1329 Circle Through Three Points(三角形外心)

    题目链接 抄的外心模版.然后,输出认真一点.1Y. #include <cstdio> #include <cstring> #include <string> # ...

  6. 【BZOJ1208】[HNOI2004]宠物收养所 Splay

    还是模板题,两颗splay,找点删即可. #include <iostream> #include <cstdio> #include <cstdlib> #def ...

  7. 获取UILabel宽度的方法

    - (CGFloat)labelLength:(NSString *)str font:(CGFloat)font{ str = ISSTRING(str) ? str : @"" ...

  8. 20145330《Java程序设计》第一次实验报告

    20145330<Java程序设计>第一次实验报告 实验一Java开发环境的熟悉 实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用Eclipse 编辑.编译.运行.调试Ja ...

  9. 使用Uboot启动内核并挂载NFS根文件系统

    配置编译好内核之后,将生成的内核文件uImage拷贝到/tftpboot/下,通过tftp服务器将内核下载到开发板,使用命令:tftp 31000000 uImage.下载完成之后配置bootargs ...

  10. CAS单点登录中文用户名乱码问题

    CAS单点登录中文用户名乱码问题,有两种情况 1. CAS server乱码 即在向server端提交用户名和密码时,发生了乱码,解决方法是: 打开WEB-INF/web.xml,在其它的Filter ...