LightOJ 1289 LCM from 1 to n (bitmap screen prime labeled +

https://vjudge.net/contest/324284#problem/B

Water math problem, in fact, would like to write a bitmap. . And shaped like pressure

Meaning of the questions: to let n seek lcm (1,2,3, ..., n), n <= 1e8

Thinking: Obviously ans = n less than all primes p [i] is max (p [i] ^ k) is multiplied. Because space is too large, the number of array elements installed no less than open, use bitmaps, int can save 32-bit binary, we can each as a number, but also because in addition to the even number 2 is not a prime number, so just odd screening .

L(1) = 1

L(x+1) = { L(x) * p    if x+1 is a perfect power of prime p
         { L(x)        otherwise

L(2) = 1 * 2
L(3) = 1 * 2 * 3
L(4) = 1 * 2 * 3 * 2      // because 4 = 2^2
L(5) = 1 * 2 * 3 * 2 * 5
L(6) = 1 * 2 * 3 * 2 * 5  // 6 is not a perfect power of a prime
L(7) = 1 * 2 * 3 * 2 * 5 * 7




#include <bits / STDC ++ H.> 
the using namespace STD; 
typedef unsigned int the UI; 
const int MAXN = 100 000 005; 
const int N = 5.8 million; 
the UI MUL [N]; 
int VIS [MAXN / 32 + 10], P [N] ; 
int CNT, n-; 
void the init () 
{ 
    CNT =. 1; 
    P [0] = MUL [0] = 2; 
    for (int I =. 3; I <MAXN; I + = 2) 
        ! IF ((VIS [I / 32] & (<<. 1 ((i / 2)% 16)))) 
        {// find where i is a representative, not accounting for the even bits 
            P [CNT] = i; 
            MUL [CNT] = MUL [CNT -1] * I; 
            for (int. 3 J = I *; J <MAXN; = 2 * I + J) 
                VIS [J / 32] | = (. 1 << ((J / 2)% 16)); // delete the factor of the number of bits 
            cnt ++; 
        } 
        // printf ( "% d \ the n-", cnt); 
}
UI solve ()
{ 
    Int POS = upper_bound, (P, P + CNT, n) - P -. 1; // find the maximum is smaller than n prime 
    the UI ANS = MUL [POS]; 
    for (int I = 0; I <CNT && P [I ] * P [I] <= n-; I ++) 
    { 
        int TEM = P [I]; 
        int tt P = [I] * P [I]; // this is likely to overflow tt int 
        the while (tt / TEM == P [I] && TT <= n-) 
        { 
                TEM * = P [I]; 
                TT * = P [I]; 
        } 
        ANS * = TEM / P [I]; 
    } 
    return ANS; 
} 
int main () 
{ 
    int T, 0 = L; 
    the init (); 
    Scanf ( "% D", & T);  
    the while (T -) 
    { 
        Scanf ( "% D", & n-);
        the printf ( "% Case D: U% \ n-", L ++, Solve ()); 
    } 
    return 0; 
}

 



Guess you like

Origin www.cnblogs.com/wzgg/p/11479902.html