题目链接: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. iOS学习31之UITableVIewCell自定义

    1. 自定义Cell 1> 为什么要自定义Cell UITableView 中系统的Cell共提供了四种默认样式,  分别是: UITableViewCellStyleDefault UITab ...

  2. BZOJ3740 : pku2842 N-dimension Matching N维匹配

    做$n$次递推求出$n$维Hash值即可,时间复杂度$O(nk)$. #include<cstdio> #define N 500010 int n,i,j,t,delta,ca,cb,a ...

  3. 【HDU3652】B-number 数位DP

    B-number Problem Description A wqb-number, or B-number for short, is a non-negative integer whose de ...

  4. BZOJ 2049 & LCT又一模板

    题意: 自己看...加边删边问联通... SOL: 就加了一个findroot而已... 然而时间还是惨不忍睹...优化全开也才1700ms...膜seter... Code: /*========= ...

  5. hdu2612 Find a way

    Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. L ...

  6. Android中使用Handler造成内存泄露

    1.什么是内存泄露? Java使用有向图机制,通过GC自动检查内存中的对象(什么时候检查由虚拟机决定),如果GC发现一个或一组对象为不可到达状态,则将该对象从内存中回收.也就是说,一个对象不被任何引用 ...

  7. 《深入浅出Windows 10通用应用开发》

        <深入浅出Windows 10通用应用开发>采用Windows 10的SDK进行重新改版,整合了<深入浅出Windows Phone 8.1应用开发>和<深入解析 ...

  8. 【BZOJ】2456: mode

    http://www.lydsy.com/JudgeOnline/problem.php?id=2456 题意:给一个$n<=500000$的数列,求出现次数超过$\lfloor \frac{n ...

  9. BZOJ2322: [BeiJing2011]梦想封印

    Description 渐渐地,Magic Land上的人们对那座岛屿上的各种现象有了深入的了解. 为了分析一种奇特的称为梦想封印(Fantasy Seal)的特技,需要引入如下的概念: 每一位魔法的 ...

  10. java ReentrantLock可重入锁功能

    1.可重入锁是可以中断的,如果发生了死锁,可以中断程序 //如下程序出现死锁,不去kill jvm无法解决死锁 public class Uninterruptible { public static ...