LCM from 1 to n

题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26999


题意:给定一个正整数n,求的值,输入数据有10000组,每组一个数n,1<=n<=10^8。


分析:定义为1,2,3,...,n的最小公倍数。则可以发现有如下结论:




所以有:


LCM from 1 to n_第1张图片


那么,我们先把所有素数的连乘预处理出来,然后再对每一个素数的整数次幂根据n的不同进行操作。在素数筛选中,我们利用位图来节省内存空间。


#include 
#include 
#include 
#include 
#include 

using namespace std;
typedef unsigned int uint;
const int N = 100000005;
const int M = 6000005;
const int SHIFT = 5;
const int RADIX = (1 << SHIFT) - 1;

int flag[(N>>SHIFT)+1];
uint sum[M];
int p[M];
int k;

inline void SetBit(int x)
{
    flag[x>>SHIFT] |= (1<<(x&RADIX));
}

inline bool GetBit(int x)
{
    return flag[x>>SHIFT] & (1<<(x&RADIX));
}

void isprime()
{
    k = 0;
    for(int i=2; i



你可能感兴趣的:(数论)